Response helpers make it easy to send data or errors back from your server code in a clear way.
Response helpers (json, error) in Svelte
import { json, error } from '@sveltejs/kit'; // To send JSON data return json(data, { status: 200 }); // To send an error response throw error(statusCode, 'Error message');
The json helper formats your data as JSON and sets the right headers.
The error helper throws an error with a status code and message that SvelteKit understands.
import { json } from '@sveltejs/kit'; export function GET() { const data = { message: 'Hello world' }; return json(data); }
import { error } from '@sveltejs/kit'; export function GET() { throw error(404, 'Not found'); }
import { json } from '@sveltejs/kit'; export function POST() { const newItem = { id: 1, name: 'Item 1' }; return json(newItem, { status: 201 }); }
This server GET handler checks for an 'id' query parameter. If missing, it throws a 400 error. Otherwise, it returns JSON data with the item info.
import { json, error } from '@sveltejs/kit'; export function GET({ url }) { const id = url.searchParams.get('id'); if (!id) { throw error(400, 'Missing id parameter'); } const data = { id, name: `Item ${id}` }; return json(data); }
Always use throw error() to send error responses so SvelteKit can handle them properly.
The json() helper automatically sets the Content-Type header to application/json.
You can customize the HTTP status code in both helpers to match your API needs.
Use json() to send JSON data from server endpoints.
Use error() to throw HTTP errors with messages.
These helpers keep your server responses clear and consistent.
Practice
json() helper in SvelteKit server endpoints?Solution
Step 1: Understand the role of
Thejson()json()helper formats data as a JSON response for the client.Step 2: Differentiate from other helpers
Unlikeerror(), which throws HTTP errors,json()sends data successfully.Final Answer:
To send a JSON response back to the client -> Option CQuick Check:
json()sends JSON data [OK]
- Confusing json() with error()
- Thinking json() parses requests
- Assuming json() redirects
Solution
Step 1: Check json() syntax
Thejson()helper takes data as first argument and an options object with status as second.Step 2: Validate options object usage
return json({ message: 'Success' }, { status: 200 }); correctly uses{ status: 200 }as the second argument.Final Answer:
return json({ message: 'Success' }, { status: 200 }); -> Option DQuick Check:
json(data, { status }) syntax [OK]
- Passing status as a separate argument without object
- Using error() to send success responses
- Using Response constructor incorrectly
import { json, error } from '@sveltejs/kit';
export function GET() {
const data = { user: 'Alice' };
if (!data.user) {
throw error(404, 'User not found');
}
return json(data);
}What will be the HTTP status code and response if
data.user is missing?Solution
Step 1: Analyze the condition
Ifdata.useris missing (falsy), the code throwserror(404, 'User not found').Step 2: Understand error() behavior
Theerror()helper throws an HTTP error with the given status and message.Final Answer:
Status 404 with message 'User not found' -> Option BQuick Check:
Missing user triggers error(404) [OK]
- Assuming json() runs even if error is thrown
- Thinking error() returns JSON data
- Confusing status codes for errors
import { json, error } from '@sveltejs/kit';
export function POST() {
const data = { success: true };
return error(200, 'All good');
}Solution
Step 1: Check status code validity
Theerror()helper requires status codes 400-599. Using 200 triggers "Non-error status code 200".Step 2: Correct helper for success
For success (status 200), usejson()instead oferror().Final Answer:
Using error() with status 200 is incorrect -> Option AQuick Check:
Use json() for success, error() for errors [OK]
- Using error() with 200 status
- Not returning json() for success
- Assuming error() can send normal data
Solution
Step 1: Check error throwing for empty list
export function GET() { const items = []; if (items.length === 0) { throw error(400, 'No items found'); } return json(items); } correctly throwserror(400, 'No items found')when the list is empty.Step 2: Validate response for non-empty list
It returnsjson(items)properly when items exist.Final Answer:
export function GET() { const items = []; if (items.length === 0) { throw error(400, 'No items found'); } return json(items); } -> Option AQuick Check:
Throw error() for HTTP errors, json() for data [OK]
- Returning error() instead of throwing it
- Using json() with error status instead of throwing error()
- Throwing json() which is invalid
Ready to prove it?
Pass the test and this pattern moves unknown
