- Prefect Cloud:
- Self-hosted Prefect server:
- Interactive REST API documentation for self-hosted Prefect server is available under Server API on the sidebar navigation or at
http://localhost:4200/docsor the/docsendpoint of the PREFECT_API_URL you have configured to access the server. You must have the server running withprefect server startto access the interactive documentation.
- Interactive REST API documentation for self-hosted Prefect server is available under Server API on the sidebar navigation or at
Interact with the REST API
You can interact with the Prefect REST API in several ways:- Create an instance of
PrefectClient, which is part of the Prefect Python SDK. - Use your favorite Python HTTP library such as Requests or HTTPX
- Use an HTTP library in your language of choice
- Use curl from the command line
PrefectClient with self-hosted Prefect server
This example usesPrefectClient with self-hosted Prefect server:
Requests with Prefect
This example uses the Requests library with Prefect Cloud to return the five newest artifacts.curl with Prefect Cloud
This example uses curl with Prefect Cloud to create a flow run:--data-raw "{}" is required and is where you can specify other aspects of the flow run such as the state. Windows users substitute ^ for \ for line multi-line commands.
Finding your Prefect Cloud details
When working with the Prefect Cloud REST API you will need your Account ID and often the Workspace ID for the workspace you want to interact with. You can find both IDs for a Prefect profile in the CLI withprefect profile inspect my_profile. This command will also display your Prefect API key, as shown below:
https://app.prefect.cloud/account/abc-my-account-id-is-here/workspaces/123-my-workspace-id-is-here.
REST guidelines
The REST APIs adhere to the following guidelines:- Collection names are pluralized (for example,
/flowsor/runs). - We indicate variable placeholders with colons:
GET /flows/:id. - We use snake case for route names:
GET /task_runs. - We avoid nested resources unless there is no possibility of accessing the child resource outside the parent context. For example, we query
/task_runswith a flow run filter instead of accessing/flow_runs/:id/task_runs. - The API is hosted with an
/api/:versionprefix that (optionally) allows versioning in the future. By convention, we treat that as part of the base URL and do not include that in API examples. - Filtering, sorting, and pagination parameters are provided in the request body of
POSTrequests where applicable.- Pagination parameters are
limitandoffset. - Sorting is specified with a single
sortparameter. - See more information on filtering below.
- Pagination parameters are
HTTP verbs
GET,PUTandDELETErequests are always idempotent.POSTandPATCHare not guaranteed to be idempotent.GETrequests cannot receive information from the request body.POSTrequests can receive information from the request body.POST /collectioncreates a new member of the collection.GET /collectionlists all members of the collection.GET /collection/:idgets a specific member of the collection by ID.DELETE /collection/:iddeletes a specific member of the collection.PUT /collection/:idcreates or replaces a specific member of the collection.PATCH /collection/:idpartially updates a specific member of the collection.POST /collection/actionis how we implement non-CRUD actions. For example, to set a flow run’s state, we usePOST /flow_runs/:id/set_state.POST /collection/actionmay also be used for read-only queries. This is to allow us to send complex arguments as body arguments (which often cannot be done viaGET). Examples includePOST /flow_runs/filter,POST /flow_runs/count, andPOST /flow_runs/history.
Filter results
Objects can be filtered by providing filter criteria in the body of aPOST request. When multiple criteria are specified, logical AND will be applied to the criteria.
Filter criteria are structured as follows:
objects is the name of the collection to filter over (for example, flows). The collection can be either the object being queried for (flows for POST /flows/filter) or a related object (flow_runs for POST /flows/filter).
object_field is the name of the field over which to filter (name for flows). Note that some objects may have nested object fields, such as {flow_run: {state: {type: {any_: []}}}}.
field_operator_ is the operator to apply to a field when filtering. Common examples include:
any_: return objects where this field matches any of the following values.is_null_: return objects where this field is or is not null.eq_: return objects where this field is equal to the following value.all_: return objects where this field matches all of the following values.before_: return objects where this datetime field is less than or equal to the following value.after_: return objects where this datetime field is greater than or equal to the following value.
"database" and failed flow runs, POST /flows/filter with the following request body: