Prompt
How do I combine multiple APIs into one endpoint?
Latest observation
To combine multiple APIs into one endpoint, you usually build a backend “facade” or aggregation layer that calls each API, merges the results, and returns a single response to the client.
Common approaches
1) Backend aggregation endpoint
Create your own endpoint, for example:
GET /dashboard-data
This endpoint:
- Receives one request from the client
- Calls multiple APIs internally
- Combines the responses
- Returns one unified JSON payload
This is the most common and flexible approach.
2) API Gateway / BFF pattern
If you have multiple clients (web, mobile, etc.), use:
- API Gateway for routing, auth, rate limiting, and sometimes response composition
- BFF (Backend for Frontend) if each client needs different combined data
3) Serverless function / edge function
If the logic is simple, you can use:
- AWS Lambda
- Cloudflare Workers
- Vercel/Netlify functions
These can act as a lightweight aggregation endpoint.
Example in Node.js / Express
import express from "express";
const app = express();
app.get("/combined", async (req, res) => {
try {
const [usersRes, postsRes, commentsRes] = await Promise.all([
fetch("https://api.example.com/users"),
fetch("https://api.example.com/posts"),
fetch("https://api.example.com/comments")
]);
const [users, posts, comments] = await Promise.all([
usersRes.json(),
postsRes.json(),
commentsRes.json()
]);
res.json({
users,
posts,
comments
});
} catch (err) {
res.status(500).json({ error: "Failed to fetch combined data" });
}
});
app.listen(3000);
Important considerations
Performance
- Use
Promise.all()/ parallel requests when APIs are independent - Add caching if responses don’t change often
Error handling
Decide what to do if one API fails:
- Fail the whole endpoint
- Return partial data
- Return fallback values
Example partial response:
const results = await Promise.allSettled([
fetch("https://api1.com/data"),
fetch("https://api2.com/data")
]);
Data transformation
You may need to normalize different API formats into one consistent schema.
Authentication
If the APIs require different keys/tokens, store them securely on the server, not in the client.
Rate limits
Be careful: one client request may trigger multiple upstream requests.
Example response shape
{
"user": {
"id": 1,
"name": "Alice"
},
"orders": [
{ "id": 101, "total": 49.99 }
],
"recommendations": [
{ "id": 201, "title": "Product A" }
]
}
Best practice recommendation
If you’re building a real app:
- Put the composition logic on the server
- Keep the client calling one endpoint
- Use parallel fetches, caching, and error handling
- Consider GraphQL if you need highly flexible aggregation
If you want, I can show you how to do this in:
- Node.js / Express
- Python / FastAPI
- Next.js
- AWS Lambda
- GraphQL