0Pricing
Sveltejs Academy · Lesson

Returning JSON Responses

Use the json() helper to return structured JSON with the correct content-type.

The json Helper

Use json() from @sveltejs/kit to return JSON with proper headers.

import { json } from "@sveltejs/kit";
return json({ ok: true });

Status Codes

Pass a second options object to control status and additional headers.

return json({ ok: false }, { status: 400 });

All lessons in this course

  1. +server.js: GET POST PUT DELETE
  2. Request and RequestEvent
  3. Returning JSON Responses
  4. Streaming with ReadableStream
← Back to Sveltejs Academy