📖 Scenario: You are building a simple Svelte app that needs to get data from the server. Instead of putting data directly in the page, you will create an API route that sends data as JSON. This is how real apps separate data from the page and allow dynamic updates.
🎯 Goal: Create a SvelteKit API route that returns a JSON list of products. Then fetch this data in a Svelte page and display the product names.
📋 What You'll Learn
Create an API route file src/routes/api/products/+server.js that returns JSON data
Define a list of products as an array of objects with id and name
Create a config variable for the API path in the page component
Fetch the product data from the API route using fetch
Display the product names in a list on the page
💡 Why This Matters
🌍 Real World
Web apps often get data from backend APIs. Using API routes to serve JSON lets the frontend fetch data dynamically and update the UI without reloading the whole page.
💼 Career
Understanding API routes and data fetching is essential for frontend and full-stack developers working with modern frameworks like SvelteKit, React, or Next.js.
Progress0 / 4 steps
1
Create the product data array
In the file src/routes/api/products/+server.js, create a constant called products that is an array with these exact objects: { id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }, and { id: 3, name: 'Cherry' }.
Svelte
Hint
Use export const products = [...] to create the array of product objects.
2
Add the API route handler
In src/routes/api/products/+server.js, add an exported function called GET that returns a JSON response with the products array.
Svelte
Hint
Use export function GET() and return json(products) to send JSON data.
3
Set the API path in the page component
In the Svelte page file src/routes/+page.svelte, create a constant called apiPath and set it to the string '/api/products'.
Svelte
Hint
Define const apiPath = '/api/products'; to hold the API URL.
4
Fetch and display products in the page
In src/routes/+page.svelte, write an async function called loadProducts that fetches from apiPath, converts the response to JSON, and stores it in a products variable. Then use a {#each} block to display each product's name in an unordered list.
Svelte
Hint
Use fetch inside an async function and onMount to load data. Then display with {#each}.
Practice
(1/5)
1. Why do API routes in SvelteKit serve data endpoints instead of full HTML pages?
easy
A. To replace the need for any frontend code
B. To handle user authentication only
C. To serve static files like images and stylesheets
D. To provide data that frontend components can use without reloading the whole page
Solution
Step 1: Understand the purpose of API routes
API routes are designed to send data, not full pages, so frontend can update parts dynamically.
Step 2: Compare with full page reloads
Serving data endpoints avoids reloading the entire page, improving user experience and speed.
Final Answer:
To provide data that frontend components can use without reloading the whole page -> Option D
Quick Check:
API routes serve data, not full pages [OK]
Hint: API routes send data, not full pages, for smooth updates [OK]
Common Mistakes:
Thinking API routes serve full HTML pages
Confusing API routes with static file servers
Assuming API routes only handle authentication
2. Which of the following is the correct way to define a GET handler in a SvelteKit API route?
easy
A. export function get() { return { body: { message: 'Hello' } }; }
B. export async function GET() { return new Response('Hello'); }
C. function GET() { return 'Hello'; }
D. export const get = () => 'Hello';
Solution
Step 1: Recall SvelteKit API route syntax
Handlers must be exported functions named after HTTP methods in uppercase, e.g., GET.
Step 2: Check return type
Handlers return a Response object or an object with status and body; returning a string alone is invalid.
Final Answer:
export async function GET() { return new Response('Hello'); } -> Option B
Quick Check:
Use uppercase method names and return Response [OK]
Hint: Use uppercase method names and return Response objects [OK]
Common Mistakes:
Using lowercase method names like get instead of GET
Returning plain strings instead of Response or object
Not exporting the handler function
3. Given this SvelteKit API route code, what will be the JSON response body?
A. The function should return a Response object or use 'return json()' helper
B. The return object should use 'json' instead of 'body'
C. The function must be async
D. The function name should be lowercase 'get'
Solution
Step 1: Check return type requirements
SvelteKit expects API handlers to return a Response object or use the 'json()' helper for JSON responses.
Step 2: Identify incorrect return format
Returning { body: { message: 'Hi' } } alone is not valid; it must be wrapped properly.
Final Answer:
The function should return a Response object or use 'return json()' helper -> Option A
Quick Check:
Return Response or use json() helper [OK]
Hint: Return Response or use json() helper for JSON data [OK]
Common Mistakes:
Returning plain objects without wrapping
Using lowercase function names
Assuming async is always required
5. You want to create an API route in SvelteKit that returns a list of users as JSON. Which approach correctly serves this data endpoint with proper headers and JSON format?