restatedev/sdk-typescript
Loading repository data...
The Restate SDK lets you implement handlers. Handlers can be part of a Basic Service, a Virtual Object, or a Workflow. This page shows how to define them with the TypeScript SDK.
The Restate TypeScript SDK is open source (MIT).
Two API flavors
The TypeScript SDK offers two ways to write durable handlers:- Standard TypeScript and Promises use familiar
async/awaitsyntax for simple, readable code. Use this API for sequential logic, parallel individual steps, and calls to other handlers. - Generator-based API uses TypeScript generator functions (
function*andyield*) for more flexible concurrency and type-safe composition of operations, tasks, and their results. It is a good fit for coding agents or developers familiar with JavaScript and TypeScript generators. Use Durable Coroutines to run concurrent tasks with multiple durable steps in one handler, or define whole handlers as generators.
execute.
The remainder of these docs uses the Promises-based API for code snippets, except where the generator-based API is explicitly introduced.
Prerequisites
Getting started
Add the@restatedev/restate-sdk dependency to your project to start developing Restate services.
Basic Services
Basic Services group related handlers and expose them as callable endpoints:- Define a service using
restate.service. - The service has a name and a list of handlers.
- Each handler has a name and can be called at
<RESTATE_INGRESS>/myService/myHandler - Handlers take the
Contextas the first argument. - Handlers can take one optional JSON-serializable input and must return a JSON-serializable output (see custom serialization for advanced types).
- Serve the service over HTTP (port
9080by default).
Virtual Objects
Virtual Objects are services that are stateful and key-addressable — each object instance has a unique ID and persistent state.- Define a Virtual Object using
restate.object(...) - Each instance is identified by a key (accessible via
ctx.key). - Virtual Objects can have exclusive and shared handlers.
- Exclusive handlers receive an
ObjectContext, allowing read/write access to object state. - Shared handlers are wrapped in
handlers.object.shared(...)and use theObjectSharedContext - Serve the Virtual Object over HTTP (port
9080by default).
Workflows
Workflows are long-lived processes with a defined lifecycle. They run once per key and are ideal for long-running, multi-step processes that need to preserve progress, wait for external events, or accept input while they are running.- Define a workflow with
restate.workflow(...) - Every workflow must include a
runhandler:- This is the main orchestration entry point
- It runs exactly once per workflow execution and uses the
WorkflowContext - Resubmission of the same workflow will fail with “Previously accepted”. The invocation ID can be found in the request header
x-restate-id. - Use
ctx.keyto access the workflow’s unique ID
- Additional handlers must use the
WorkflowSharedContext. They can query state or resolve workflow promises, run concurrently with therunhandler, and remain callable until the retention time expires. - Serve the Workflow over HTTP (port
9080by default).