[KongchangAI]
· 3 min read· 1,611 words

Getting Started with FastAPI: From Frontend-Backend Separation to RESTful API in Practice

Getting Started with FastAPI: From Frontend-Backend Separation to RESTful API in Practice

Organize the essential Web development patterns, API conventions, and RESTful core concepts for getting started with FastAPI.

Based on a Bilibili introductory tutorial, this article organizes the conceptual Web development framework that should be established before learning FastAPI. It distinguishes non-separated from separated frontend-backend patterns, notes that the latter is used by over 90% of enterprise projects, and explains how FastAPI is built on Starlette and designed for API development, making it suitable for AI Agent backends. It also introduces RESTful conventions and recommends understanding concepts before FastAPI and Pydantic practice.

FastAPI is one of the most closely watched frameworks in Python backend development in recent years. Thanks to its high performance and its developer-friendly design for API work, it has become a common choice for building the backend services of AI Agents. This article is based on an introductory tutorial from a Bilibili creator, organizing the core concepts of FastAPI—from Web development patterns to RESTful API conventions—to help you quickly build a knowledge framework for backend development.

Why Understanding Web Development Patterns Is a Prerequisite for Learning FastAPI

Many beginners jump straight into writing endpoints, only to get stuck on their understanding of "frontend-backend separation." To grasp FastAPI's positioning, you first need to understand the difference between the two basic patterns in Web development.

Non-Separated Frontend and Backend: One Server Does It All

Non-separated frontend and backend is an earlier development pattern. Everything the browser sees—interface, data, effects—is provided by the same application server. The server needs to simultaneously handle business logic, query the database, render HTML pages, and finally return a complete HTML response.

HTML rendering flow under the non-separated frontend-backend pattern

The problem with this pattern is straightforward: low development efficiency, since the interface design must be completed first before the backend can start stitching together data and rendering logic; at the same time, team collaboration cannot be split, and frontend and backend can only proceed serially.

Frontend-Backend Separation: Frontend Handles Display, Backend Handles Data

Frontend-backend separation divides responsibilities into two parts: the frontend server (also called a static file server) stores static resources such as HTML, CSS, JS, and images; the Python application server specifically handles business logic and exposes a unified API interface externally. When the browser requests the interface, it accesses the frontend server; when it requests data, it accesses the backend API—these two directions of requests are distributed.

According to the creator, more than 90% of enterprise-level Web projects currently adopt the frontend-backend separation pattern. Moreover, backend services are not limited to Web sites—mobile apps and WeChat Mini Programs are essentially "interface + backend data" structures as well, and the backend simply needs to return data in JSON format uniformly. According to statistics, more than 95% of server-side responses for Web applications and mobile applications use JSON, while XML was the mainstream solution a decade ago.

JSON has become the mainstream data exchange format, covering Web and mobile applications

JSON (JavaScript Object Notation) is a lightweight data exchange format that describes data using key-value pairs and array structures. Its syntax originates from JavaScript object literals, but it is language-independent, and almost all programming languages provide parsing and generation support. Compared with XML, JSON is more compact in structure, more readable, and has lower parsing overhead, so it has replaced XML as the de facto standard in Web and mobile interfaces. The json module in Python's standard library and Pydantic, which FastAPI depends on, are both designed around JSON data structures for serialization and validation logic. Understanding JSON's nested structures (objects, arrays, strings, numbers, booleans, null) is the foundation for reading interface return values and designing request bodies, and it is also the direct prerequisite for learning Pydantic data models later.

FastAPI's Positioning: Built for APIs, but Not Limited to APIs

FastAPI is built on top of the Starlette framework. Starlette was designed from the beginning for API interface development, so FastAPI is naturally suited to frontend-backend separated projects.

FastAPI is built on top of the Starlette framework

It also supports server-side rendering through template engines and can be used for non-separated frontend-backend projects, but that is not its main focus. The official FastAPI documentation is very clear about its positioning: API backend development as the core. In the tutorial, the creator describes it as "one of the fastest Web backend frameworks," which is also why it is widely adopted in AI Agent development scenarios—Agent services usually need high-performance asynchronous interfaces to connect with large model call chains.

Starlette is a lightweight Python Web framework/toolkit based on ASGI (Asynchronous Server Gateway Interface), providing low-level capabilities such as routing, middleware, request and response objects, WebSocket, and background tasks. ASGI is an asynchronous gateway interface standard compared with the traditional WSGI, supporting async/await coroutines and enabling a single process to concurrently handle a large number of I/O-waiting requests (such as calling large model APIs or accessing databases). This is one of the sources of FastAPI's high-concurrency performance. FastAPI does not reimplement this low-level layer; instead, it is built on Starlette, then adds Pydantic for data validation and automatically generates OpenAPI documentation. Therefore, "FastAPI is built on top of Starlette" means that its routing and asynchronous capabilities are inherited from Starlette, while parameter validation, serialization, and documentation generation come from the Pydantic and OpenAPI ecosystems.

RESTful API: The Mainstream Convention for Backend Development

API (Application Programming Interface) is the entry point an application provides externally for data operations. It can be a function, a class, or a URL address. The client calls this entry point by sending a request.

There are currently two mainstream API interface conventions: REST and RPC. The market share of RPC interface protocols is no longer high, and REST has become the absolute mainstream due to its simplicity and security.

REST has become the mainstream API convention due to its simplicity and security

REST stands for Representational State Transfer, often translated into Chinese as "具象状态转化." In the tutorial, the creator admits that this Chinese translation "makes no sense at all." It is a resource-oriented programming pattern—treating all API interfaces as resources, and requesting an interface means obtaining a resource (data).

In practice, REST distinguishes operation types through HTTP request methods:

  • POST: Add a resource, such as adding a student (POST /student)
  • GET: Retrieve resources, such as querying all students (GET /students) or querying a single student (GET /students/1)
  • PUT/PATCH: Modify a resource, such as updating the information of the student with ID 1
  • DELETE: Delete a resource, such as deleting the student with ID 1

The URL path identifies the resource object being operated on, and the request method identifies the operation type. The combination of the two forms a complete API semantics.

RPC (Remote Procedure Call) is another interface design approach, whose core is to disguise method calls to remote services as local function calls. The client focuses on "which method to call and what parameters to pass," rather than operating on a certain resource. Common implementations include gRPC, JSON-RPC, Thrift, and others. Among them, gRPC is still widely used in internal microservice communication and high-performance binary transmission scenarios, and has not completely exited the market. The key difference between REST and RPC lies in the level of abstraction: REST is resource-oriented, using URLs to locate resources and HTTP methods to express actions, with intuitive semantics and easy caching; RPC is procedure-oriented, and interfaces are usually verb-like method names, which may offer higher transmission efficiency but stronger coupling. When the tutorial says "RPC's market share is no longer high," it mainly refers to the field of open APIs facing the public internet, while RPC remains common in internal service-to-service communication.

Learning Advice: Concepts First, Then Practice

The structure of this tutorial is to first use one lesson to quickly popularize concepts such as Web development patterns, APIs, and REST, and then move into hands-on coding with FastAPI and Pydantic. The creator suggests that students without a Django background first supplement their basic understanding of frontend-backend separation, while those with a background can quickly skip the concept part and go directly into the framework coding section.

The reason FastAPI's learning curve is so gentle is precisely that it turns RESTful conventions into code constraints at the framework level—the process of writing an interface is itself the practice of REST design philosophy. For Python developers who want to enter AI Agent backend development, mastering FastAPI is a highly cost-effective step.

Pydantic is Python's data validation and serialization library. It declares data models through type annotations and automatically performs type conversion, validation, and error reporting at runtime. FastAPI uses it extensively to define request body and response body structures: you write a class that inherits from BaseModel, and the framework can automatically parse the request JSON, validate fields, generate interface documentation, and perform response serialization. This gives RESTful's concept of "resources" a clear carrier when it lands at the code level—a Pydantic model often corresponds to the data structure of one resource. At the same time, Pydantic's error messages are clear, which can significantly reduce the cost of interface debugging. It is a core component in the FastAPI learning path that is just as important as routing.

One-line Summary

Organize the essential Web development patterns, API conventions, and RESTful core concepts for getting started with FastAPI.

Paragraph Summary

Based on a Bilibili introductory tutorial, this article organizes the conceptual Web development framework that should be established before learning FastAPI. First, it distinguishes between non-separated and separated frontend-backend patterns, pointing out that the latter is the choice of more than 90% of current enterprise-level projects: the frontend server handles static resources, while the backend server focuses on business logic and returns JSON data. Second, it positions FastAPI: it is built on Starlette, naturally designed for API development, and also supports template rendering, but its official positioning centers on API backends, and its high-performance asynchronous interfaces make it suitable for AI Agent backend scenarios. Finally, it introduces RESTful conventions, treating interfaces as resources, using HTTP methods such as POST/GET/PUT/DELETE to express operation types, and URLs to identify resource objects. The article emphasizes understanding concepts first before moving into FastAPI and Pydantic practice, with a gentle learning curve.

#FastAPI#RESTful API#frontend-backend separation#Python backend development#Starlette#Pydantic#AI Agent backend
Share:

Related articles