Your Restate handler can call other handlers in three ways:
To call a service from an external application, see the HTTP or Kafka documentation.

Request-response calls

To call a Restate handler and wait for its result:
# import my_service # Import the service module to get to the handler, not the service itself

# To call a Service:
response = await ctx.service_call(my_service.my_handler, arg="Hi")

# To call a Virtual Object:
response = await ctx.object_call(my_object.my_handler, key="Mary", arg="Hi")

# To call a Workflow:
# `run` handler — can only be called once per workflow ID
response = await ctx.workflow_call(my_workflow.run, key="my_workflow_id", arg="Hi")
# Other handlers can be called anytime within workflow retention
response = await ctx.workflow_call(
    my_workflow.interact_with_workflow, key="my_workflow_id", arg="Hi"
)
Use generic calls when you don’t have the service definition or need dynamic service names:
response = await ctx.generic_call(
    "MyObject", "my_handler", key="Mary", arg=json.dumps("Hi").encode("utf-8")
)
After a workflow’s run handler completes, other handlers can still be called for up to 24 hours (default). Update this via the service configuration.
Request-response calls between exclusive handlers of Virtual Objects may lead to deadlocks:
  • Cross deadlock: A → B and B → A (same keys).
  • Cycle deadlock: A → B → C → A.
Use the UI or CLI to cancel and unblock deadlocked invocations.

Sending messages

To send a message to another Restate handler without waiting for a response:
# To message a Service:
ctx.service_send(my_service.my_handler, arg="Hi")

# To message a Virtual Object:
ctx.object_send(my_object.my_handler, key="Mary", arg="Hi")

# To message a Workflow:
# `run` handler — can only be called once per workflow ID
ctx.workflow_send(my_workflow.run, key="my_wf_id", arg="Hi")
# Other handlers can be called anytime within workflow retention
ctx.workflow_send(my_workflow.interact_with_workflow, key="my_wf_id", arg="Hi")
Restate handles message delivery and retries, so the handler can complete and return without waiting for the message to be processed. Use generic send when you don’t have the service definition:
ctx.generic_send("MyService", "my_handler", arg=json.dumps("Hi").encode("utf-8"))
Calls to a Virtual Object execute in order of arrival, serially. Example:
ctx.object_send(my_object.my_handler, key="Mary", arg="I'm call A")
ctx.object_send(my_object.my_handler, key="Mary", arg="I'm call B")
Call A is guaranteed to execute before B. However, other invocations may interleave between A and B.

Delayed messages

To send a message after a delay:
# To message a Service with a delay:
ctx.service_send(my_service.my_handler, arg="Hi", send_delay=timedelta(hours=5))

# To message a Virtual Object with a delay:
ctx.object_send(
    my_object.my_handler, key="Mary", arg="Hi", send_delay=timedelta(hours=5)
)

# To message a Workflow with a delay:
ctx.workflow_send(
    my_workflow.run, key="my_workflow_id", arg="Hi", send_delay=timedelta(hours=5)
)
Use generic send with a delay when you don’t have the service definition:
ctx.generic_send(
    "MyService",
    "my_handler",
    arg=json.dumps("Hi").encode("utf-8"),
    send_delay=timedelta(hours=5),
)
Learn how this is different from sleeping and then sending a message.

Using an idempotency key

To prevent duplicate executions of the same call, add an idempotency key:
await ctx.service_call(
    my_service.my_handler,
    arg="Hi",
    idempotency_key="my-idempotency-key",
)
Restate automatically deduplicates calls made during the same handler execution, so there’s no need to provide an idempotency key in that case. However, if multiple handlers might call the same service independently, you can use an idempotency key to ensure deduplication across those calls.

Attach to an invocation

To wait for or get the result of a previously sent message:
# Send a request, get the invocation id
handle = ctx.service_send(
    my_service.my_handler, arg="Hi", idempotency_key="my-idempotency-key"
)
invocation_id = await handle.invocation_id()

# Now re-attach
result = await ctx.attach_invocation(invocation_id)
  • With an idempotency key: Wait for completion and retrieve the result.
  • Without an idempotency key: Can only wait, not retrieve the result.

Cancel an invocation

To cancel a running handler:
ctx.cancel_invocation(invocation_id)

See also

  • Error Handling: Handle failures and terminal errors in service calls
  • Durable Timers: Implement timeouts for your service calls
  • Serialization: Customize how data is serialized between services
  • Sagas: Roll back or compensate for canceled service calls.