Skip to main content

Overview

The PrefectClient offers methods to simplify common operations against Prefect’s REST API that may not be abstracted away by the SDK. For example, to reschedule flow runs, one might use methods like:
  • read_flow_runs with a FlowRunFilter to read certain flow runs
  • create_flow_run_from_deployment to schedule new flow runs
  • delete_flow_run to delete a very Late flow run

Getting a client

By default, get_client() returns an asynchronous client to be used as a context manager, but you may also use a synchronous client.

Pagination and default query limits

Client methods that accept limit and offset parameters - such as read_flow_runs, read_deployments, and read_task_runs — are subject to a server-side maximum. When limit is None (the default), the server applies PREFECT_API_DEFAULT_LIMIT, which defaults to 200. To retrieve all matching records, paginate with offset:

Configure custom headers

You can configure custom HTTP headers to be sent with every API request by setting the PREFECT_CLIENT_CUSTOM_HEADERS setting. This is useful for adding authentication headers, API keys, or other custom headers required by proxies, CDNs, or security systems.

Setting custom headers

Custom headers can be configured via environment variables or settings. The headers are specified as key-value pairs in JSON format.
Protected headersCertain headers are protected and cannot be overridden by custom headers for security reasons:
  • User-Agent - Managed by Prefect to identify client version
  • Prefect-Csrf-Token - Used for CSRF protection
  • Prefect-Csrf-Client - Used for CSRF protection
If you attempt to override these headers, Prefect will log a warning and ignore the custom header value.

Examples

These examples are meant to illustrate how one might develop their own utilities for interacting with the API.
If you believe a client method is missing, or you’d like to see a specific pattern better represented in the SDK generally, please open an issue.

Reschedule late flow runs

To bulk reschedule flow runs that are late, delete the late flow runs and create new ones in a Scheduled state with a delay. This is useful if you accidentally scheduled many flow runs of a deployment to an inactive work pool, for example. The following example reschedules the last three late flow runs of a deployment named healthcheck-storage-test to run six hours later than their original expected start time. It also deletes any remaining late flow runs of that deployment. First, define the rescheduling function:
Then use it to reschedule flows:
reschedule_late_flows.py

Get the last N completed flow runs from your workspace

To get the last N completed flow runs from your workspace, use read_flow_runs and prefect.client.schemas. This example gets the last three completed flow runs from your workspace:
Use it to get the last 3 completed runs:
get_recent_flows.py
Instead of the last three from the whole workspace, you can also use the DeploymentFilter to get the last three completed flow runs of a specific deployment.

Transition all running flows to cancelled through the Client

Use get_clientto set multiple runs to a Cancelled state. This example cancels all flow runs that are in Pending, Running, Scheduled, or Late states when the script is run.
Cancel all pending, running, scheduled or late flows:
cancel_flows.py

Query events with pagination

Query historical events from the Prefect API with support for filtering and pagination. This is useful for analyzing past activity, debugging issues, or building custom monitoring tools. The following example queries events from the last hour and demonstrates how to paginate through results:
query_events.py

Create, read, or delete artifacts

Create, read, or delete artifacts programmatically through the Prefect REST API. With the Artifacts API, you can automate the creation and management of artifacts as part of your workflow. For example, to read the five most recently created Markdown, table, and link artifacts, you can run the following:
fixture:mock_post_200
If you don’t specify a key or that a key must exist, you will also return results, which are a type of key-less artifact. See the Prefect REST API documentation on artifacts for more information.