3.3 Example: API Request
User sends request to API.
API queries Database.
Database returns result β API β User.
1. User Sends Request to API
To bring together what youβve learned about sequence diagrams, letβs build a simple but realistic example: a user sends a request to an API. This is one of the most common patterns in software β a user does something (like clicking βSubmitβ), which triggers a chain of messages between systems.
β
What This Example Shows
In this example: 1οΈβ£ The User sends a request to the Frontend (like a web page or mobile app). 2οΈβ£ The Frontend forwards that request to the API Server. 3οΈβ£ The API Server processes the request and checks the Database if needed. 4οΈβ£ The API Server returns a response to the Frontend. 5οΈβ£ The Frontend shows the result to the User.
β
The Mermaid Code
Hereβs how youβd write this sequence diagram in Mermaid:
```mermaid
sequenceDiagram
actor User
participant Frontend
participant API
participant Database
User->>Frontend: Click Submit
Frontend->>API: Send API Request
API->>Database: Query Data
Database-->>API: Return Data
API-->>Frontend: Send Response
Frontend-->>User: Show Result
```β
Whatβs Happening Here
Participants:
Useris anactor(a human).Frontend,API, andDatabaseareparticipants(system parts).
Messages:
User->>Frontendβ The user initiates an action.Frontend->>APIβ The frontend calls the backend API.API->>Databaseβ The API queries the database.Database-->>APIβ The database returns data (dashed arrow for response).API-->>Frontendβ The API replies to the frontend.Frontend-->>Userβ The frontend shows the final result to the user.
β
What This Diagram Communicates
Who starts the interaction: The user.
What path the request follows: From the frontend to the API to the database β and back.
How responses travel: Dashed arrows show the flow of data back to the user.
The whole diagram reads top to bottom, like a timeline.
β
Where to Use This
Use a diagram like this to:
Explain your appβs request flow to teammates.
Document an API call in your project wiki.
Show clients or non-technical stakeholders how your system handles user input.
In short:
π A simple sequence diagram makes it easy to show how a userβs action travels through your system, step by step β crystal clear for developers, testers, and non-tech readers alike.
2. API Queries Database
In many modern applications, when a user makes a request through an API β like logging in, fetching their profile, or getting product details β the API usually needs to fetch or update data in a database. This step is a key part of any sequence diagram for backend systems.
β
Why Does the API Query the Database?
The API Server is the middle layer that:
Receives the request from the frontend or client.
Validates or processes the request.
Queries the Database to read or write data.
Packages the response and sends it back up the chain.
In your sequence diagram, showing this step makes it clear where data is stored and which component owns it.
β
Mermaid Syntax: API and Database
In Mermaid, this interaction is just another message arrow β it shows:
The API calls the Database.
The Database returns a result.
Example snippet:
->>means the API requests data.-->>means the Database returns data.
β
Putting It in Context
Hereβs how the full diagram looks with the API-Database step included:
β
What Happens in This Step
When the Frontend sends the userβs request, the API might need more information than it has on hand.
The API sends a query to the Database β for example, βIs this username valid?β or βFetch all items in this category.β
The Database checks its records and returns the matching data.
The API uses this data to build a final response for the user.
β
Why Itβs Important to Show
This step:
Makes it clear that the API does not store data itself β it delegates that to the Database.
Shows where the data comes from when building a response.
Helps developers and architects see possible points of failure, like slow queries or missing data.
Is useful for debugging and performance tuning β you know exactly when and where the Database is involved.
β
Tips for Real-World Diagrams
βοΈ Use clear, simple labels like Query Data, Fetch Record, or Write Data to describe what the API is doing. βοΈ If you have multiple database calls, show each one in order β itβs fine to have multiple arrows back and forth. βοΈ Combine with notes to explain what kind of data is being read or written if needed.
In short:
π The API β Database step is the backbone of how modern apps work β your sequence diagram shows exactly when the data is requested, how it flows, and who handles it before itβs returned to the user.
3. Database Returns Result β API β User
After the API Server queries the Database, the final part of the interaction is to return the data back through the chain β from the Database to the API, then to the Frontend, and finally back to the User.
This step completes the request-response cycle and shows exactly how information flows back to the person or system that asked for it.
β
Why Is This Step Important?
When you diagram this return path, you:
Make it clear which system is responsible for processing the result.
Show how raw database data is handled or formatted by the API before reaching the user.
Highlight where you can add things like error handling, validation, or caching.
Without this final return flow, the diagram would be incomplete β the whole point is to show the round trip of the request and response.
β
Mermaid Syntax: Returning the Result
Hereβs the key part in Mermaid syntax:
-->>uses a dashed arrow to show the response path.The Database sends data to the API.
The API processes it and forwards it to the Frontend.
The Frontend displays it for the User.
β
Full Example in Context
Hereβs the complete code block again for clarity:
β
How This Final Step Works
1οΈβ£ Database responds to the API with the requested data β for example, a user profile or a product list. 2οΈβ£ The API might transform this data (e.g., filter fields, add status codes, or wrap it in JSON). 3οΈβ£ The API sends the response to the Frontend. 4οΈβ£ The Frontend takes that response and renders it for the User β showing a success message, updating the page, or displaying an error if something went wrong.
β
Key Takeaways
βοΈ The Database only stores and returns data β it doesnβt talk to the User directly. βοΈ The API acts as the middle layer that knows how to talk to the Database and how to reply to the Frontend. βοΈ The Frontend makes sure the User gets a clear, usable result.
β
Why It Matters in a Sequence Diagram
It shows the entire lifecycle of a request.
It highlights dependencies β for example, if the Database is slow, the Userβs wait time increases.
It documents exactly where responses flow, which is helpful for troubleshooting, design discussions, and explaining the system to others.
In short:
π Database β API β Frontend β User ties your whole interaction together β your sequence diagram clearly shows how data travels back to the person who started the request.
Last updated