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 HTTP path for the API endpoint | |
string | The HTTP method for the API endpoint (GET, POST, PUT, DELETE, etc.) | |
object | Schema for validating the request body. For TypeScript/JavaScript steps, it uses zod schemas. For Python steps, it uses Pydantic models. | |
array | Optional middleware functions to run before the handler |
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.