The following APIs are the same as the Web APIS you already know. Additionally, we follow the WinterCG conventions.
Handler
The only required code to make your Function runnable is to export a handler
function, that accepts a Request
and returns a Response
(or a promise returning a Response):
export function handler(request: Request): Response {
return new Response('Hello World!');
}
Starting from this simple code, you can do whatever you whish, using the Web APIs you already know.
Global objects
console
Similar to the the standard console
object on the browser and on Node.js, expect that it only supports the following methods:
log
info
debug
warn
error
You can log multiple objects, and use string substitution. See the documentation on MDN.
process
The only usage of process
is to get environment variables via process.env
.
Example:
export function handler(request: Request): Response {
return new Response(`My secret is: ${process.env.SECRET}`);
}
crypto
The standard crypto
object.
crypto.randomUUID()
The standard randomUUID()
method. See the documentation on MDN.
crypto.getRandomValues()
The standard getRandomValues()
method. See the documentation on MDN.
crypto.subtle
The standard CryptoSubtle
object. See the documentation on MDN.
The following table summarizes the supported algorithms on each method:
sign() , verify() | encrypt() , decrypt() | digest() | deriveBits() , deriveKey() | wrapKey() , unwrapKey() | |
---|---|---|---|---|---|
HMAC | ✅ | ||||
SHA-256 | ✅ | ||||
SHA-384 | ✅ | ||||
SHA-512 | ✅ | ||||
AES-GCM | ✅ | ✅ |
TextEncoder
The standard TextEncoder
object. See the documentation on MDN.
TextDecoder
The standard TextDecoder
object. See the documentation on MDN.
AbortController
The standard AbortController
object. See the documentation on MDN.
AbortSignal
The standard AbortSignal
object. See the documentation on MDN.
Fetch
fetch()
method? Jump to fetch().Request
The standard Request
object. See the documentation on MDN.
Response
The standard Response
object. See the documentation on MDN.
Streaming:
You can pass a ReadableStream
object as the body
of a Response
to stream the response as more data becomes available. Often, you won't need to implement the logic yourself as it is implemented by the frameworks and libraries you use.
URL
The standard URL
object. See the documentation on MDN.
URLSearchParams
The standard URLSearchParams
object. See the documentation on MDN.
Headers
The standard Headers
object. See the documentation on MDN.
Streams
ReadableStream
The standard ReadableStream
object. See the documentation on MDN.
ReadableStreamDefaultReader
The standard ReadableStreamDefaultReader
object. See the documentation on MDN.
WritableStream
The standard WritableStream
object. See the documentation on MDN.
WritableStreamDefaultWriter
The standard WritableStreamDefaultWriter
object. See the documentation on MDN.
TransformStream
The standard TransformStream
object. See the documentation on MDN.
Global methods
fetch()
The standard fetch
method. See the documentation on MDN.
atob()
The standard atob
method. See the documentation on MDN.
btoa()
The standard btoa
method. See the documentation on MDN.