API Design in Front-End Systems

The API layer is the bridge between your frontend and the backend server. Whenever your app needs to load data, create new posts, update content, or listen for changes, it communicates with the server through APIs. The protocol you choose for this communication depends on the type of application you are building and its real-time requirements. Good API design starts with that choice, and MDN’s HTTP overview is a useful refresher.
Please spend no more than 10% of your interview explaining this portion.
Choosing the Right API Protocol
The choice of API protocol comes down to the application’s needs. Here’s a quick reference table of common front-end application types and the protocols that make the most sense for each:
| App Type | Example Feature | Recommended API Protocol |
|---|---|---|
| Chat Application | Real-time messaging between users | WebSockets |
| News Feed | Scrolling to load posts, pulling new content | HTTP REST |
| Live Notifications | Instant alerts or status updates | Server-Sent Events (SSE) or WebSockets |
| Collaborative Editing | Multiple users editing the same document | WebSockets |
| AI/Streaming Response | Streaming generated text (like ChatGPT) | Server-Sent Events (SSE) |
| Profile or Settings Management | Fetching and updating user preferences | HTTP REST or GraphQL |
| Analytics Dashboard | Loading and filtering reports or metrics | HTTP REST or GraphQL |
Designing API Endpoints Example: Facebook-Style News Feed

Most data fetching can be handled with HTTP-based APIs for a typical news feed or social app. GraphQL is an excellent choice if your data is complex and clients want to specify what fields they need. Below is a set of example endpoints for a Facebook-style feed, showing how you might structure queries and mutations for getting a feed, getting a specific feed item, and creating a new post.
// Get Feed (Cursor-based pagination)
GET /api/v1/gql/feed
Input: {
cursor: string, // Optional, points to the last item from the previous fetch
limit: number, // How many items to fetch
post_types: [PostType], // filter feed items by type
// ...other filters
}
Headers: {
Authorization: string // "Bearer <token>"; the server derives the user from the token for auth and the personalized feed
}
// Response: List of FeedItems, next cursor, etc.
// Get Feed Item
GET /api/v1/gql/feed-item
Input: {
post_id: string
}
Headers: {
Authorization: string // "Bearer <token>"
}
// Response: Single FeedItem with comments and metadata
// Create Post
POST /api/v1/gql/create-feed-post
Input: {
title: String,
media: [Media],
// ...other post fields
}
Headers: {
Authorization: string // "Bearer <token>"; never trust a user ID sent by the client
}
// Response: Status code, created post ID, errors if any
Expected Response: News Feed Example
{
"data": [
{
"id": "post1",
"author": { "id": "user1", "name": "Alice" },
"content": "Hello world",
"createdAt": "2024-05-19T18:24:00Z",
"media": [{ "url": "...", "type": "image" }],
"comments": [
{ "id": "comment1", "author": { "id": "user2", "name": "Bob" }, "text": "Nice post!" }
]
},
// more posts...
],
"nextCursor": "YXJyYXljb25uZWN0aW9uOjEw", // base64 encoded cursor for next fetch
"hasMore": true
}
Why Cursor-Based Pagination?
Cursor-based pagination is preferred for news feeds and infinite scrolling because it is more robust when data changes rapidly. Unlike page-based pagination (pages 1, 2, and 3), which can show duplicate or missing results if new posts are added or old ones are deleted between requests, cursor-based pagination always starts from the last item received. This keeps the data consistent as the user scrolls and ensures smooth UX even as new posts arrive.
Cursor-based approaches are also more scalable for large datasets and prevent issues with users seeing the same content twice or skipping content when the feed updates quickly. The server returns a cursor pointing to the last item received, which the client uses to fetch the next set of results.
Best Practices for API Design
- Version your API: Always include a version number in your API path, such as
/api/v1/..., to avoid breaking changes for clients when you need to evolve your API over time. - Consistent naming conventions: Use clear, predictable endpoint names and stick to a consistent case (camelCase or snake_case).
- Authentication: Pass authentication tokens using secure headers (or httpOnly cookies), and let the server derive the user from the token. Avoid sensitive data in URLs or query parameters.
- Explicit status codes: Always return the correct HTTP status codes for success, errors, or unauthorized requests (200, 201, 400, 401, 404, 500, etc).
- Clear error messages: Provide meaningful error objects with error codes and messages so the frontend can handle problems gracefully.
- Flexible filtering and sorting: Allow clients to filter, sort, and paginate data to reduce unnecessary payloads and improve performance.
- Minimal over-fetching: With GraphQL or well-designed REST, the client can specify which fields to use to reduce bandwidth and improve speed.
- Documentation: Keep API contracts well-documented and updated so frontend and backend teams stay in sync.
Good API design keeps your frontend flexible, makes your system easy to debug, and allows you to scale easily as your product grows.
API Request Headers and Response Format
Every API call your frontend makes should clearly define what headers are sent and what response body is expected in return. Proper headers ensure security, user personalization, and reliable communication. The response structure should be predictable and easy to transform into your client-side data store.
Common Request Headers
- Authorization:
Bearer <token>– used for authenticating the user session. - Content-Type:
application/json– specifies the format of the request body (only needed when you send one, like a POST). - X-User-ID: User identifier for tracking or personalization (when sessions are stateless). Never trust it for authentication; the server should verify identity from the token.
- Accept-Language: Sets the preferred language for localized responses.
- X-Request-ID: Optional, used for tracing and debugging across distributed systems.
GET /api/v1/gql/feed
Authorization: Bearer eyJhbGciOi...
Accept: application/json
X-User-ID: user_12345
Accept-Language: en-US
X-Request-ID: 123e4567-e89b-12d3-a456-426614174000
Expected Response Body
The response body should return all necessary information for rendering the UI and updating your client data model. Here’s a sample for a feed endpoint:
{
"data": [
{
"id": "post_1",
"author": {
"id": "user_123",
"name": "Alice"
},
"content": "Hello world",
"createdAt": "2024-05-19T18:24:00Z",
"media": [
{ "url": "https://cdn.example.com/image1.jpg", "type": "image" }
],
"comments": [
{
"id": "comment_1",
"author": {
"id": "user_456",
"name": "Bob"
},
"text": "Nice post!",
"createdAt": "2024-05-19T18:25:00Z"
}
]
}
// ...more posts
],
"nextCursor": "YXJyYXljb25uZWN0aW9uOjEw",
"hasMore": true
}
Transforming the Response on the Client
When the response arrives, map and normalize the data to match your local client data model. Extract users, posts, and comments into separate entities for easier updates and caching. This makes rendering faster and future data changes more predictable.
Next up, let’s finally discuss the O in R.A.D.I.O, which is Optimizations.
