Static JSON REST APIs with SvelteKit
Exposing the blog's content as prerendered JSON endpoints: a read-only API that needs no server at all.
A static site can still offer an API: you just generate the JSON files at build time.
Prerendered endpoints
In SvelteKit a +server.ts file with prerender = true runs only once, at build time, and its output is saved to disk.
export const prerender = true;
export const GET = () =>
new Response(JSON.stringify(getPosts()), {
headers: { 'Content-Type': 'application/json' }
});Routes with parameters
For dynamic routes such as /api/posts/[slug].json you need to export the entries function, which lists every value to generate.
Available endpoints
Each language has its own endpoints (English ones live under /en/api/):
/api/posts.json/api/posts/<slug>.json/api/categories.json/api/tags.json/api/authors.json/api/authors/<nick>.json