Low-Level HTTP Request Handler
Actors can handle HTTP requests through the onRequest handler.
For most use cases, actions provide high-level API powered by HTTP that's easier to work with than low-level HTTP. However, low-level handlers are required when implementing custom use cases or integrating external libraries that need direct access to the underlying HTTP Request/Response objects or WebSocket connections.
Handling HTTP Requests
The onRequest handler processes HTTP requests sent to your actor. It receives the actor context and a standard Request object and returns a Response object.
See also the raw fetch handler example.
Sending Requests To Actors
Via RivetKit Client
Use the .fetch() method on an actor handle to send HTTP requests to the actor's onRequest handler. This can be executed from either your frontend or backend.
Via HTTP API
This handler can be accessed with raw HTTP using https://api.rivet.dev/gateway/{actorId}/request/{...path}.
For example, to call POST /increment on the counter actor above:
The request is routed to the actor's onRequest handler where:
request.methodis"POST"request.urlends with/increment(the path after/request/)- Headers, body, and other request properties are passed through unchanged
See the HTTP API reference for more information on HTTP routing and authentication.
Via Proxying Requests
You can proxy HTTP requests from your own server to actor handlers using the RivetKit client. This is useful when you need to add custom authentication, rate limiting, or request transformation before forwarding to actors.
Connection & Lifecycle Hooks
onRequest will trigger the onBeforeConnect, onConnect, and onDisconnect hooks. Read more about lifecycle hooks.
Requests in flight will be listed in c.conns. Read more about connections.
WinterTC Compliance
The onRequest handler is WinterTC compliant and will work with existing libraries using the standard Request and Response types.
Limitations
- Does not support streaming responses & server-sent events at the moment. See the tracking issue.
OPTIONSrequests currently are handled by Rivet and are not passed toonRequest
API Reference
RequestContext- Context for HTTP request handlersActorDefinition- Interface for defining request handlers