Getting started
Web standards
Edit this page on GitHubThroughout this documentation, you'll see references to the standard Web APIs that SvelteKit builds on top of. Rather than reinventing the wheel, we use the platform, which means your existing web development skills are applicable to SvelteKit. Conversely, time spent learning SvelteKit will help you be a better web developer elsewhere.
These APIs are available in all modern browsers and in many non-browser environments like Cloudflare Workers, Deno and Vercel Edge Functions. During development, and in adapters for Node-based environments (including AWS Lambda), they're made available via polyfills where necessary (for now, that is — Node is rapidly adding support for more web standards).
In particular, you'll get comfortable with the following:
Fetch APIspermalink
SvelteKit uses fetch
for getting data from the network. It's available in hooks and server routes as well as in the browser.
A special version of
fetch
is available inload
functions, server hooks and API routes for invoking endpoints directly during server-side rendering, without making an HTTP call, while preserving credentials. (To make credentialled fetches in server-side code outsideload
, you must explicitly passcookie
and/orauthorization
headers.) It also allows you to make relative requests, whereas server-sidefetch
normally requires a fully qualified URL.
Besides fetch
itself, the Fetch API includes the following interfaces:
Requestpermalink
An instance of Request
is accessible in hooks and server routes as event.request
. It contains useful methods like request.json()
and request.formData()
for getting data that was posted to an endpoint.
Responsepermalink
An instance of Response
is returned from await fetch(...)
and handlers in +server.js
files. Fundamentally, a SvelteKit app is a machine for turning a Request
into a Response
.
Headerspermalink
The Headers
interface allows you to read incoming request.headers
and set outgoing response.headers
. For example, you can get the request.headers
as shown below, and use the json
convenience function to send modified response.headers
:
ts
import {json } from '@sveltejs/kit';/** @type {import('./$types').RequestHandler} */export functionGET ({request }) {// log all headersconsole .log (...request .headers );// create a JSON Response using a header we receivedreturnjson ({// retrieve a specific headeruserAgent :request .headers .get ('user-agent')}, {// set a header on the responseheaders : { 'x-custom-header': 'potato' }});}
ts
import {json } from '@sveltejs/kit';import type {RequestHandler } from './$types';export constGET :RequestHandler = ({request }) => {// log all headersconsole .log (...request .headers );// create a JSON Response using a header we receivedreturnjson ({// retrieve a specific headeruserAgent :request .headers .get ('user-agent'),},{// set a header on the responseheaders : { 'x-custom-header': 'potato' },},);};
FormDatapermalink
When dealing with HTML native form submissions you'll be working with FormData
objects.
ts
import {json } from '@sveltejs/kit';/** @type {import('./$types').RequestHandler} */export async functionPOST (event ) {constbody = awaitevent .request .formData ();// log all fieldsconsole .log ([...body ]);returnjson ({// get a specific field's valuename :body .get ('name') ?? 'world'});}
ts
import {json } from '@sveltejs/kit';import type {RequestHandler } from './$types';export constPOST :RequestHandler = async (event ) => {constbody = awaitevent .request .formData ();// log all fieldsconsole .log ([...body ]);returnjson ({// get a specific field's valuename :body .get ('name') ?? 'world',});};
Stream APIspermalink
Most of the time, your endpoints will return complete data, as in the userAgent
example above. Sometimes, you may need to return a response that's too large to fit in memory in one go, or is delivered in chunks, and for this the platform provides streams — ReadableStream, WritableStream and TransformStream.
URL APIspermalink
URLs are represented by the URL
interface, which includes useful properties like origin
and pathname
(and, in the browser, hash
). This interface shows up in various places — event.url
in hooks and server routes, $page.url
in pages, from
and to
in beforeNavigate
and afterNavigate
and so on.
URLSearchParamspermalink
Wherever you encounter a URL, you can access query parameters via url.searchParams
, which is an instance of URLSearchParams
:
ts
constfoo =url .searchParams .get ('foo');
Web Cryptopermalink
The Web Crypto API is made available via the crypto
global. It's used internally for Content Security Policy headers, but you can also use it for things like generating UUIDs:
ts
constuuid =crypto .randomUUID ();