Skip to main content

Overview

The Restate TypeScript SDK is open source and can be found on GitHub: (sdk-typescript repo).

πŸš€Set up your project in seconds

Have a look at the TypeScript Quickstart!

Add the @restatedev/restate-sdk dependency to your NodeJS project to start developing Restate services.

The Restate SDK lets you implement handlers. Handlers can either be part of a Service or of a Virtual Object. Let's have a look at how to define them.

Services​

Services and their handlers are defined as follows:


_17
import * as restate from "@restatedev/restate-sdk";
_17
_17
const myService = restate.service({
_17
name: "MyService",
_17
handlers: {
_17
myHandler: async (ctx: restate.Context, greeting: string) => {
_17
return `${greeting}!`;
_17
},
_17
}
_17
})
_17
_17
export const MyService: typeof myService = { name: "MyService" };
_17
_17
restate
_17
.endpoint()
_17
.bind(myService)
_17
.listen();

  • Create the service
  • Specify the service name. The service can then be called at <RESTATE_INGRESS_URL>/MyService/myHandler.
  • List the handlers. Each handler has a name (myHandler) and a function that implements the handler logic. The function has the Restate Context as its first argument. Within the handler, you use the Context to interact with Restate. The SDK stores the actions you do on the context in the Restate journal to make them durable.
  • The handler input parameters and return type are optional and can be of any type, as long as they can be serialized as a Buffer with Buffer.from(JSON.stringify(yourObject)) and deserialized with JSON.parse(result.toString()) as T.
  • Export the service definition so that it can be used by other handlers to call the service. (See Service Communication docs.)
  • Create an endpoint and bind the service(s) to the Restate endpoint. Listen on the specified port (default 9080) for connections and requests.

Virtual Objects​

Virtual objects and their handlers are defined similarly to services, with the following differences:


_17
import * as restate from "@restatedev/restate-sdk";
_17
_17
const myVirtualObject = restate.object({
_17
name: "MyVirtualObject",
_17
handlers: {
_17
myHandler: async (ctx: restate.ObjectContext, greeting: string) => {
_17
return `${greeting} ${ctx.key}!`;
_17
},
_17
}
_17
})
_17
_17
export const MyVirtualObject: typeof myVirtualObject = { name: "MyVirtualObject" };
_17
_17
restate
_17
.endpoint()
_17
.bind(myVirtualObject)
_17
.listen();

  • Create the virtual object
  • The handlers use the ObjectContext instead of the Context.

Now that you have a high-level idea of what a Restate service might look like, let's have a look at what the Restate Context allows you to do.