﻿<head><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4357510538660394"
     crossorigin="anonymous"></script></head>> ## Documentation Index
> Fetch the complete documentation index at: https://docs.restate.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Services

> Implementing Restate services with the TypeScript SDK.

The Restate SDK lets you implement **handlers**. Handlers can be part of a **[Basic Service](https://docs.restate.dev/foundations/services.md#basic-service)**, a **[Virtual Object](https://docs.restate.dev/foundations/services.md#virtual-object)**, or a **[Workflow](https://docs.restate.dev/foundations/services.md#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`/`await` syntax 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*` and `yield*`) 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](https://docs.restate.dev/develop/ts/concurrent-tasks.md#durable-coroutines) to run concurrent tasks with multiple durable steps in one handler, or [define whole handlers as generators](https://docs.restate.dev/develop/ts/concurrent-tasks.md#defining-generator-handlers).

Both APIs support Services, Virtual Objects, and Workflows. You can also run coroutines inside an existing Promises-based handler with `execute`.

The remainder of these docs uses the **Promises-based API** for code snippets, except where the generator-based API is explicitly introduced.

## Prerequisites

* [NodeJS](https://nodejs.org/en/) >= v22 or [Bun](https://bun.sh/docs/installation)
* [npm CLI](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) >= 9.6.7

## Getting started

> [!TIP]
>
> Get started quickly with the [TypeScript Quickstart](https://docs.restate.dev/quickstart.md).

Add the [`@restatedev/restate-sdk`](https://www.npmjs.com/package/@restatedev/restate-sdk) dependency to your project to start developing Restate services.

## Basic Services

[Basic Services](https://docs.restate.dev/foundations/services.md) group related **handlers** and expose them as callable endpoints:

```ts {"CODE_LOAD::ts/src/develop/service.ts"}  theme={null}
import * as restate from "@restatedev/restate-sdk";

export const myService = restate.service({
  name: "MyService",
  handlers: {
    myHandler: async (ctx: restate.Context, greeting: string) => {
      return `${greeting}!`;
    },
  },
});

restate.serve({ services: [myService] });
```

* Define a service using [`restate.service`](https://restatedev.github.io/sdk-typescript/functions/_restatedev_restate-sdk.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 [`Context`](https://restatedev.github.io/sdk-typescript/interfaces/_restatedev_restate-sdk.Context) as the first argument.
* Handlers can take one optional JSON-serializable input and must return a JSON-serializable output (see [custom serialization](https://docs.restate.dev/develop/ts/serialization.md) for advanced types).
* Serve the service over HTTP (port `9080` by default).

## Virtual Objects

[Virtual Objects](https://docs.restate.dev/foundations/services.md) are services that are stateful and key-addressable — each object instance has a unique ID and persistent state.

```ts {"CODE_LOAD::ts/src/develop/virtual_object.ts"}  theme={null}
import * as restate from "@restatedev/restate-sdk";

export const myObject = restate.object({
  name: "MyObject",
  handlers: {
    myHandler: async (ctx: restate.ObjectContext, greeting: string) => {
      return `${greeting} ${ctx.key}!`;
    },
    myConcurrentHandler: restate.handlers.object.shared(
      async (ctx: restate.ObjectSharedContext, greeting: string) => {
        return `${greeting} ${ctx.key}!`;
      }
    ),
  },
});

restate.serve({ services: [myObject] });
```

* Define a Virtual Object using [`restate.object(...)`](https://restatedev.github.io/sdk-typescript/functions/_restatedev_restate-sdk.object)
* Each instance is identified by a key (accessible via `ctx.key`).
* Virtual Objects can have [exclusive and shared handlers](https://docs.restate.dev/foundations/handlers.md#handler-behavior).
* Exclusive handlers receive an [`ObjectContext`](https://restatedev.github.io/sdk-typescript/interfaces/_restatedev_restate-sdk.ObjectContext), allowing read/write access to object state.
* Shared handlers are wrapped in `handlers.object.shared(...)` and use the [`ObjectSharedContext`](https://restatedev.github.io/sdk-typescript/interfaces/_restatedev_restate-sdk.ObjectSharedContext)
* Serve the Virtual Object over HTTP (port `9080` by default).

## Workflows

[Workflows](https://docs.restate.dev/foundations/services.md) 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.

```ts {"CODE_LOAD::ts/src/develop/workflow.ts"}  theme={null}
import * as restate from "@restatedev/restate-sdk";

export const myWorkflow = restate.workflow({
  name: "MyWorkflow",
  handlers: {
    run: async (ctx: restate.WorkflowContext, req: string) => {
      // implement workflow logic here

      return "success";
    },

    interactWithWorkflow: async (ctx: restate.WorkflowSharedContext) => {
      // implement interaction logic here
      // e.g. resolve a promise that the workflow is waiting on
    },
  },
});

restate.serve({ services: [myWorkflow] });
```

* Define a workflow with [`restate.workflow(...)`](https://restatedev.github.io/sdk-typescript/functions/_restatedev_restate-sdk.workflow)
* Every workflow **must** include a `run` handler:
  * This is the main orchestration entry point
  * It runs exactly once per workflow execution and uses the [`WorkflowContext`](https://restatedev.github.io/sdk-typescript/interfaces/_restatedev_restate-sdk.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.key` to access the workflow's unique ID
* Additional handlers must use the [`WorkflowSharedContext`](https://restatedev.github.io/sdk-typescript/interfaces/_restatedev_restate-sdk.WorkflowSharedContext). They can query state or resolve workflow promises, run concurrently with the `run` handler, and remain callable until the retention time expires.
* Serve the Workflow over HTTP (port `9080` by default).

## Configuring services

Check out the [service configuration docs](https://docs.restate.dev/services/configuration.md) to learn how to configure service behavior, including timeouts and retention policies.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.