If you've ever tried to explain a REST API workflow to a teammate and ended up drawing boxes on a whiteboard that looked nothing like the actual request flow, you know the frustration. PlantUML sequence diagrams solve that problem by letting you write plain-text code that turns into clear, accurate visuals of how your API endpoints interact who calls what, in what order, and what gets returned. These code examples matter because they keep your documentation in sync with your code, version-controlled, and easy to update when your API changes.
What does a PlantUML sequence diagram for a REST API actually look like?
A sequence diagram in PlantUML uses simple syntax to show participants (like a client, server, and database) and the messages they exchange over time. For REST API workflows, each message usually represents an HTTP request or response a GET, POST, PUT, or DELETE call flowing between components.
Here's a basic example of a login flow:
@startuml
actor User
participant "Client App" as Client
participant "API Server" as API
database "Auth DB" as DB
User -> Client : enters credentials
Client -> API : POST /api/login {email, password}
API -> DB : query user by email
DB --> API : user record
API -> API : validate password hash
API --> Client : 200 OK {token, user}
Client --> User : redirect to dashboard
@enduml
This script generates a diagram showing the full round-trip: user action, API call, database lookup, validation, and response. You can paste this into the PlantUML online server to see it rendered instantly.
Why do developers use PlantUML instead of drawing diagrams manually?
Three reasons come up again and again:
- Version control. Your diagram is a text file. It lives in your repo alongside your API code. When someone changes an endpoint, the diagram diff shows up in the same pull request.
- Speed. Writing the syntax is faster than dragging shapes in a diagramming tool once you know the basics.
- Consistency. Every diagram looks the same regardless of who writes it. No more debating whether to use rounded or square boxes.
If you're already working with UML diagram scripts in other contexts, you may find similarities with how UML diagram scripting syntax works for software architects in general. The mental model carries over.
How do you diagram a CRUD REST API workflow?
Most REST APIs revolve around Create, Read, Update, and Delete operations. Here's a sequence diagram for a typical CRUD flow on a /api/users resource:
@startuml
participant "Frontend" as FE
participant "API Gateway" as GW
participant "User Service" as SVC
database "Users DB" as DB
== CREATE ==
FE -> GW : POST /api/users {name, email}
GW -> SVC : forward request
SVC -> DB : INSERT user
DB --> SVC : user created
SVC --> GW : 201 Created {id, name, email}
GW --> FE : 201 Created
== READ ==
FE -> GW : GET /api/users/42
GW -> SVC : forward request
SVC -> DB : SELECT user WHERE id=42
DB --> SVC : user record
SVC --> GW : 200 OK {user object}
GW --> FE : 200 OK
== UPDATE ==
FE -> GW : PUT /api/users/42 {name}
GW -> SVC : forward request
SVC -> DB : UPDATE user SET name=?
DB --> SVC : rows affected
SVC --> GW : 200 OK {updated user}
GW --> FE : 200 OK
== DELETE ==
FE -> GW : DELETE /api/users/42
GW -> SVC : forward request
SVC -> DB : DELETE user WHERE id=42
DB --> SVC : rows affected
SVC --> GW : 204 No Content
GW --> FE : 204 No Content
@enduml
The == CREATE == syntax adds horizontal divider labels, which break the diagram into readable sections. This keeps long workflows from becoming a wall of arrows.
How do you show authentication and error handling in these diagrams?
Real APIs don't just return 200 OK every time. You should show error paths too, or your diagram tells an incomplete story.
@startuml actor Client participant "API Server" as API database "DB" as DB Client -> API : GET /api/orders (no token) API --> Client : 401 Unauthorized Client -> API : GET /api/orders\nAuthorization: Bearer token123 API -> API : validate JWT alt token valid API -> DB : SELECT orders for user DB --> API : order list API --> Client : 200 OK [orders] else token expired API --> Client : 401 Token Expired else insufficient permissions API --> Client : 403 Forbidden end @enduml
The alt/else/end blocks let you show branching logic exactly the kind of detail that saves someone from digging through source code to understand error responses.
What about async workflows like webhook callbacks?
Not every REST API interaction is a simple request-response. Webhooks, polling, and async processing are common. PlantUML supports this with the ... (delay) notation and notes:
@startuml
participant "Client App" as Client
participant "Payment API" as PayAPI
participant "Webhook Handler" as WH
database "Orders DB" as DB
Client -> PayAPI : POST /api/charge {amount, card}
PayAPI --> Client : 202 Accepted {transaction_id}
note right of Client : Client polls for status
...5 seconds later...
PayAPI -> WH : POST /webhook/payment-complete\n{transaction_id, status: success}
WH -> DB : UPDATE order SET status='paid'
DB --> WH : updated
Client -> PayAPI : GET /api/charge/txn_123
PayAPI --> Client : 200 OK {status: success}
@enduml
This makes the async nature of the workflow explicit. Anyone reading the diagram understands that the response doesn't come back immediately on the same connection.
How do you handle API gateway patterns and microservice chains?
When your REST API sits behind a gateway and fans out to multiple services, the diagram grows. Use grouping to keep it organized:
@startuml
participant "Mobile App" as App
participant "API Gateway" as GW
participant "User Service" as US
participant "Order Service" as OS
participant "Inventory Service" as IS
database "User DB" as UDB
database "Order DB" as ODB
App -> GW : POST /api/checkout {items, userId}
group Authentication
GW -> US : validate user session
US -> UDB : check session
UDB --> US : valid
US --> GW : user confirmed
end
group Order Creation
GW -> OS : create order {items, userId}
OS -> IS : check stock availability
IS --> OS : in stock
OS -> ODB : INSERT order
ODB --> OS : order created (id: 789)
OS --> GW : 201 Created {orderId: 789}
end
GW --> App : 201 Created {orderId: 789}
@enduml
The group keyword adds labeled boxes around related interactions. This mirrors how microservice workflows are often discussed in architecture reviews by logical phase, not by individual line.
Developers working with embedded or hardware-adjacent APIs sometimes combine sequence diagrams with state machine diagrams for embedded systems to show both the call flow and the state transitions triggered by API calls.
What common mistakes show up in PlantUML REST API diagrams?
- Showing only the happy path. If your diagram doesn't show 400, 401, 404, or 500 responses, it's misleading. Use
altblocks to show error branches. - Too many participants. More than six or seven participants makes diagrams hard to read. Combine or abstract components that aren't relevant to the flow you're documenting.
- No versioning info. If your API has v1 and v2 endpoints, note which version the diagram covers. Stale diagrams are worse than no diagrams.
- Forgetting response bodies. Just showing "200 OK" without noting what comes back makes the diagram less useful for frontend developers or API consumers.
- Mixing abstraction levels. Don't show internal function calls in the same diagram as HTTP-level API calls. Pick one level and stay consistent.
How do you organize PlantUML diagrams in a real project?
A few patterns that work well in practice:
- One diagram per endpoint group. Keep auth flows, user management, and order processing in separate
.pumlfiles. - Include files for shared elements. Use PlantUML's
!includedirective to define common participants once and reuse them across diagrams. - Generate during CI. Add a build step that converts
.pumlfiles to PNG or SVG and publishes them with your API docs. This way, the diagrams in your docs always match the committed source. - Store near the code. Keep diagram files in the same repo as your API code, ideally in a
/docs/diagramsfolder.
For a deeper look at how the scripting syntax works across different UML diagram types, the UML diagram scripting syntax guide covers the shared foundations that apply to sequence diagrams, class diagrams, and more.
Can you customize the look of these diagrams?
Yes. PlantUML supports skinning commands that change colors, fonts, and arrow styles. For REST API diagrams, a few useful ones:
skinparam backgroundColor #FEFEFE skinparam sequenceArrowThickness 2 skinparam roundcorner 10 skinparam maxmessagesize 200 skinparam responseMessageBelowArrow true
The responseMessageBelowArrow true setting places response messages below the return arrow, which reduces visual clutter in dense diagrams. You can also use #color on individual messages:
API -> Client : 200 OK {data} #Green
API -> Client : 500 Internal Server Error #Red
Color-coding status codes makes success and failure paths immediately obvious.
What's a good next step after creating your first diagram?
Once you have one working sequence diagram, expand from there. If you're also documenting how your API's data models relate to each other, combining sequence diagrams with class diagram scripts gives your team both the behavioral and structural views of the system.
Practical checklist for your first REST API sequence diagram
- List the participants client, gateway, services, databases.
- Map the main request flow as a top-to-bottom message sequence.
- Add at least one error branch using
alt/elseblocks. - Include realistic response bodies and status codes.
- Use
==section dividers orgroupblocks to separate logical phases. - Render the diagram and check that it's readable at a glance.
- Commit the
.pumlsource file to version control. - Set up automated rendering in your CI pipeline so docs stay current.
Start with a single endpoint. Write the PlantUML code, render it, share it with one teammate, and ask if it matches their understanding of the flow. That first feedback loop is where the real value of text-based diagramming shows up you can fix it in seconds, not minutes.
Uml Activity Diagram Markup Code for Microservices Architecture Scripts
How to Write Uml Class Diagrams in Mermaid.js
Uml Diagram Scripting Syntax Guide for Software Architects
Uml State Machine Diagram Code Snippets for Embedded Systems Developers
Flowchart Code Syntax Reference Guide for Software Developers
How to Write Flowchart Syntax in Mermaid Language