Skip to main content
The workerd runtime provides a rich set of global APIs that are automatically available in your workers without requiring imports.

Global objects

console
Console
Console logging methods for debugging and output.Methods: log(), debug(), info(), warn(), error(), trace(), dir(), dirxml(), table(), time(), timeEnd(), timeLog(), group(), groupEnd(), groupCollapsed(), assert(), count(), countReset(), clear()
crypto
Crypto
Cryptographic operations via the Web Crypto API.Properties:
  • subtle: SubtleCrypto interface for advanced cryptographic operations
  • getRandomValues(): Generate cryptographically strong random values
  • randomUUID(): Generate a random UUID
See Crypto API for details.
caches
CacheStorage
Cache API for storing HTTP responses.
  • default: Default cache
  • open(name): Open a named cache
scheduler
Scheduler
Task scheduling interface.
  • wait(delay): Create a promise that resolves after a delay
  • wait(delay, options): Wait with abort signal support
performance
Performance
Performance timing and measurement.
  • now(): High-resolution timestamp
  • timeOrigin: Time origin as DOMHighResTimeStamp
navigator
Navigator
Navigator object with user agent information.
  • userAgent: User agent string

Global functions

fetch()
function
Make HTTP requests.
function fetch(
  input: RequestInfo,
  init?: RequestInit
): Promise<Response>
See Fetch API for details.
addEventListener()
function
Register event listeners for worker events.
function addEventListener(
  type: string,
  listener: EventListener
): void
Supported events: fetch, scheduled, queue, alarm, trace, tail
removeEventListener()
function
Remove previously registered event listeners.
dispatchEvent()
function
Dispatch a custom event.
atob()
function
Decode a base64-encoded string.
function atob(data: string): string
btoa()
function
Encode a string to base64.
function btoa(data: string): string
setTimeout()
function
Schedule a function to run after a delay.
function setTimeout(
  handler: TimerHandler,
  timeout?: number,
  ...arguments: any[]
): number
clearTimeout()
function
Cancel a timer created with setTimeout.
setInterval()
function
Schedule a function to run repeatedly at intervals.
function setInterval(
  handler: TimerHandler,
  timeout?: number,
  ...arguments: any[]
): number
clearInterval()
function
Cancel a timer created with setInterval.
queueMicrotask()
function
Queue a microtask to be executed.
function queueMicrotask(callback: VoidFunction): void
structuredClone()
function
Create a deep copy of a value using the structured clone algorithm.
function structuredClone<T>(
  value: T,
  options?: StructuredSerializeOptions
): T

Global constructors

Request
constructor
Represents an HTTP request.See Fetch API for details.
Response
constructor
Represents an HTTP response.See Fetch API for details.
Headers
constructor
Represents HTTP headers.
URL
constructor
Parse and manipulate URLs.
new URL(url: string, base?: string | URL)
URLSearchParams
constructor
Parse and manipulate URL query strings.
ReadableStream
constructor
Represents a readable stream of data.See Streams API for details.
WritableStream
constructor
Represents a writable stream of data.See Streams API for details.
TransformStream
constructor
Represents a transform stream that can modify data.See Streams API for details.
WebSocket
constructor
WebSocket client for bidirectional communication.See WebSocket API for details.
WebSocketPair
constructor
Create a pair of connected WebSocket objects.
Event
constructor
Base class for events.
EventTarget
constructor
Base class for objects that can receive events.
AbortController
constructor
Create an abort signal for cancellable operations.
AbortSignal
constructor
Signal for cancelling asynchronous operations.
TextEncoder
constructor
Encode strings to UTF-8 byte arrays.
TextDecoder
constructor
Decode byte arrays to strings.
Blob
constructor
Represents immutable binary data.
File
constructor
Represents a file with name and metadata.
FormData
constructor
Represents HTML form data.

Example usage

// Using global fetch
const response = await fetch('https://api.example.com/data');
const data = await response.json();

// Using crypto
const uuid = crypto.randomUUID();
const randomBytes = new Uint8Array(16);
crypto.getRandomValues(randomBytes);

// Using console
console.log('Worker started');
console.error('Something went wrong', error);

// Using timers
setTimeout(() => {
  console.log('Delayed execution');
}, 1000);

// Using streams
const { readable, writable } = new TransformStream();

Module syntax

For ES modules syntax, export handlers from your main module:
export default {
  async fetch(request, env, ctx) {
    return new Response('Hello World!');
  },
  
  async scheduled(event, env, ctx) {
    // Handle scheduled events
  }
}

Service worker syntax

For service worker syntax, use addEventListener:
addEventListener('fetch', event => {
  event.respondWith(
    new Response('Hello World!')
  );
});

Build docs developers (and LLMs) love