This document explains the JWT-based authentication system used throughout the RealWorld API specification and how endpoints enforce authorization. It covers token generation, the authorization header format, authentication flows, and which endpoints require authentication versus those that accept optional authentication.
For information about the data models returned during authentication (User and Profile schemas), see Data Models & Schemas. For details on implementing authentication in the reference backend, see Authentication Implementation.
The RealWorld API uses a token-based authentication system built on JSON Web Tokens (JWT). The security scheme is defined as an API key that must be passed in the Authorization header for protected endpoints.
Sources: api/openapi.yml908-918
The API specification defines a security scheme named Token that uses the API key authentication type:
| Property | Value |
|---|---|
| Type | apiKey |
| Header Name | Authorization |
| Location | header |
| Token Format | Token xxxxxx.yyyyyyy.zzzzzz |
The JWT token must be prefixed with the literal string "Token " (with a space) when included in the Authorization header.
Sources: api/openapi.yml908-918
Registration Process:
/users with credentialsLogin Process:
/users/login with credentialsSources: api/openapi.yml22-54 api/Conduit.postman_collection.json10-177
JWT tokens are generated by the API in two scenarios:
/users)/users/login)The token is returned as part of the User schema in the response body under the token property.
The JWT follows the standard three-part structure:
Token HEADER.PAYLOAD.SIGNATURE
The prefix "Token " is required and must precede the actual JWT string.
For protected endpoints, the API must:
"Token " prefix existsSources: api/openapi.yml908-918 api/Conduit.postman_collection.json120-177
All authenticated requests must include the Authorization header in the following format:
Authorization: Token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNjg5...
Example from Postman Tests:
The test collection demonstrates proper header usage:
After successful login, the token is stored and reused across subsequent requests.
Sources: api/Conduit.postman_collection.json214-217 api/Conduit.postman_collection.json269-272
The following endpoints require the Token security scheme:
| Endpoint | Method | Purpose |
|---|---|---|
/user | GET | Get current user |
/user | PUT | Update current user |
/profiles/{username}/follow | POST | Follow a user |
/profiles/{username}/follow | DELETE | Unfollow a user |
/articles/feed | GET | Get personalized article feed |
/articles | POST | Create an article |
/articles/{slug} | PUT | Update an article |
/articles/{slug} | DELETE | Delete an article |
/articles/{slug}/comments | POST | Create a comment |
/articles/{slug}/comments/{id} | DELETE | Delete a comment |
/articles/{slug}/favorite | POST | Favorite an article |
/articles/{slug}/favorite | DELETE | Unfavorite an article |
Sources: api/openapi.yml69-70 api/openapi.yml86-87 api/openapi.yml135-136 api/openapi.yml159-160 api/openapi.yml179-180 api/openapi.yml231-232 api/openapi.yml281-282 api/openapi.yml308-309 api/openapi.yml357-358 api/openapi.yml391-392 api/openapi.yml416-417 api/openapi.yml440-441
The following endpoints are publicly accessible:
| Endpoint | Method | Purpose |
|---|---|---|
/users | POST | Register new user |
/users/login | POST | Login existing user |
/profiles/{username} | GET | Get user profile |
/articles | GET | List articles globally |
/articles/{slug} | GET | Get single article |
/articles/{slug}/comments | GET | Get article comments |
/tags | GET | Get all tags |
Sources: api/openapi.yml39-54 api/openapi.yml22-38 api/openapi.yml89-111 api/openapi.yml182-213 api/openapi.yml234-254 api/openapi.yml310-332 api/openapi.yml442-453
Some endpoints support optional authentication, where behavior differs based on whether a valid token is provided:
/articlesfavorited and following flags/articles/{slug}/profiles/{username}following: falsefollowing statusSources: api/openapi.yml182-213 api/openapi.yml234-254 api/openapi.yml89-111
The reference implementation uses a custom event handler pattern to enforce authentication:
The GET /articles endpoint demonstrates optional authentication:
The auth parameter is either:
undefined (no token provided){id: number} (valid token with user ID)The handler checks for authentication to apply personalized filtering:
Sources: apps/api/server/routes/api/articles/index.get.ts1-114
Returned when authentication is required but the token is missing or invalid:
Returned when the user is authenticated but not authorized to perform the action (e.g., trying to delete another user's article):
Sources: api/openapi.yml767-796
The test collection demonstrates stateful authentication:
{{token}} variableSources: api/Conduit.postman_collection.json10-342
The OpenAPI specification does not mandate token expiration, but implementations should include:
exp claim)Sources: api/openapi.yml908-918
When implementing authentication for a RealWorld backend:
Authorization: Token <jwt> header formatSources: api/openapi.yml908-918 api/Conduit.postman_collection.json1-342
Refresh this wiki