This is a beta course, so its structure, chapters, and examples may continue to change.
Server-side API Calls in Astro
Astro can do API/SSR, but keep “static first, minimal runtime”. Only put what must run at runtime on the server.
API routes
- Place under
src/pages/api/*.ts(or.js), exportGET/POSThandlers. - Return a
Responseobject; you can use thejson()helper. - Edge/Node runtimes depend on hosting; avoid Node-only modules if targeting Edge.
// src/pages/api/hello.ts
export async function GET() {
return new Response(JSON.stringify({ msg: "hello" }), {
headers: { "Content-Type": "application/json" },
});
}
SSR pages
- Export
prerender = falsein the page to enable SSR. - Use
Astro.requestfor headers/query, andfetchyour backend. - Good for user-context data, real-time, personalization; keep static when possible.
Middle-tier purposes
- Wrap backend APIs: auth, rate-limit, signing—protect upstream.
- Edge rendering: return personalized content close to users.
- Progressive dynamic: list static, detail or user bits via API.
Deployment notes
- For Cloudflare Pages Functions/Workers, stick to Fetch API; avoid Node-specific modules.
- Configure routing so
/api/*is not shadowed by static assets.
Pain points of traditional FE/BE split
Calling third-party APIs in a classic split setup often hits:
- CORS limits: browser same-origin blocks direct calls
- Security: API keys may leak in frontend bundles
- Complexity: need a separate backend to proxy
Example with Laravel + Vue.js:
🌐浏览器
🖥️后端服务
🔌第三方 API
🖥️后端服务
🌐浏览器
Astro’s elegant solution
Astro gives a full-stack path: frontend UI + backend API together.
Now a concrete demo:
IP 信息展示
Flow
- Frontend component calls internal
/api/ip-info - Astro API route requests external IP service
- API route returns the result
- Frontend updates UI
Benefits:
- Secrets and sensitive steps stay server-side
- No extra backend service—simpler deploy
- Full typing support for better DX
- Unified error/loading handling
Advantages recap
- Simpler code: no separate backend API needed
- Developer efficiency: full-stack in one file
- Security: API calls server-side, keys stay hidden
- Performance: supports static and incremental generation
Best practices
- Cache smartly: build-time fetch for rarely changing data
- Error handling: add proper error/loading states
- Type safety: define response types with TypeScript
---
interface IpInfo {
query: string;
country: string;
city: string;
isp: string;
}
try {
const response = await fetch('<http://ip-api.com/json/>');
const ipInfo: IpInfo = await response.json();
} catch (error) {
console.error('Failed to fetch IP info:', error);
}
---
Conclusion
With server components and API routes, Astro neatly solves API-call pain points in traditional FE/BE splits. You can ship full-stack features with concise code while keeping security and performance in check—better developer velocity, better user experience.
