ConceptsSteps
Api Step
An API step is exposed as an HTTP endpoint that acts as an entry point into your sequence of steps, or flow. It allows external systems or clients to trigger and interact with your flows through a REST API interface. Like any Motia Step, an API Step can be configured to emit events or wait for events to occur.
Config
The following properties are specific to the API Step, in addition to the common step config.
Prop | Type | Description |
---|---|---|
string | The development server api path to expose your api step handler | |
GET | POST | The HTTP method to use for the development server api endpoint | |
JSON Schema | In TS/JS we use zod to validate the request body, in Python/Ruby we use json schema to validate the request body. | |
ApiMiddleware[] | An array of middleware functions to be applied to the request before it reaches the handler. Middleware functions can perform tasks like authentication, logging, rate limiting, etc. |
The following examples showcase how to configure an API Step
Using Middleware
API Steps support middleware functions that can be applied to requests before they reach your handler. Middleware functions are completely framework-agnostic and can perform tasks such as:
- Authentication and authorization
- Request logging
- Rate limiting
- CORS handling
- Request validation
- Response transformation
Middleware Function Signature
Middleware functions receive:
req
: The API request object with body, headers, pathParams, and queryParamsctx
: The flow context with logger, state, emit, and traceIdnext
: A function to call the next middleware or handler in the chain- Call
next()
to continue to the next middleware or handler - The return value of
next()
is the response from the next middleware or handler - You can modify this response before returning it
- Call
Example Middleware Usage
Creating Custom Middleware
You can create your own middleware functions:
Need help? See our Community Resources for questions, examples, and discussions.