ESmith
07/08/2025, 12:29 PMMarvin
07/08/2025, 12:29 PMMarvin
07/08/2025, 12:30 PMbash
prefect server start --host 0.0.0.0 --port 4200 --server.api.auth_string="admin:password"
2. Client Configuration:
For client processes that need to communicate with the server, set these environment variables:
bash
export PREFECT_API_URL="<http://my-prefect.com/api>"
export PREFECT_API_AUTH_STRING="admin:password"
Alternatively, you can configure this in your Python code:
python
from prefect.settings import Setting
from prefect.client.schemas.actions import AuthType
Setting(name="PREFECT_API_URL", value="<http://my-prefect.com/api>").save()
Setting(name="PREFECT_API_AUTH_STRING", value="admin:password").save()
Important notes:
- Replace "admin:password" with your actual credentials
- Do NOT set PREFECT_API_KEY
when using basic auth - this will cause authentication errors
- The auth string format is always username:password
- Make sure to use the same credentials on both server and client sides
You can verify your authentication is working by accessing your server's API documentation at <http://my-prefect.com/docs>
and using the "Authorize" button with your credentials.
For additional security settings and configurations, you can refer to the security settings documentation.ESmith
07/08/2025, 12:34 PMdef trigger_prefect_flow():
url = f"{PREFECT_API_URL}/deployments/{PREFECT_FLOW_ID}/run"
headers = {
"Authorization": f"Bearer {PREFECT_API_KEY}",
"Content-Type": "application/json"
}
response = <http://requests.post|requests.post>(url, headers=headers)
if response.ok:
print("Prefect flow triggered successfully.")
else:
print(f"Failed to trigger Prefect flow. Status: {response.status_code}, Response: {response.text}")
Marvin
07/08/2025, 12:34 PMMarvin
07/08/2025, 12:34 PMpython
import base64
import requests
def trigger_prefect_flow():
# Basic auth credentials
username = "admin"
password = "password"
# Create basic auth header
credentials = f"{username}:{password}"
encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
url = f"{PREFECT_API_URL}/deployments/{PREFECT_FLOW_ID}/run"
headers = {
"Authorization": f"Basic {encoded_credentials}",
"Content-Type": "application/json"
}
response = <http://requests.post|requests.post>(url, headers=headers)
if response.ok:
print("Prefect flow triggered successfully.")
else:
print(f"Failed to trigger Prefect flow. Status: {response.status_code}, Response: {response.text}")
Key changes made:
- Removed the Bearer token authentication (which is for Prefect Cloud)
- Added basic auth using base64 encoding (this is how HTTP Basic Auth works)
- The credentials are encoded as base64(username:password)
Alternatively, you can use requests' built-in basic auth support, which is even cleaner:
python
import requests
def trigger_prefect_flow():
username = "admin"
password = "password"
url = f"{PREFECT_API_URL}/deployments/{PREFECT_FLOW_ID}/run"
headers = {
"Content-Type": "application/json"
}
response = <http://requests.post|requests.post>(
url,
headers=headers,
auth=(username, password)
)
if response.ok:
print("Prefect flow triggered successfully.")
else:
print(f"Failed to trigger Prefect flow. Status: {response.status_code}, Response: {response.text}")
Both approaches will work the same way - the second version is preferred as it lets the requests library handle the auth header construction for you. Just remember to replace "admin" and "password" with your actual credentials.
Also, make sure your PREFECT_API_URL
and PREFECT_FLOW_ID
variables are properly defined before running this code.ESmith
07/08/2025, 12:54 PMMarvin
07/08/2025, 12:54 PMMarvin
07/08/2025, 12:54 PMPOST /api/deployments/{deployment_id}/create_flow_run
So your code should be updated to:
python
import requests
def trigger_prefect_flow(deployment_id):
username = "admin"
password = "password"
url = f"{PREFECT_API_URL}/api/deployments/{deployment_id}/create_flow_run"
headers = {
"Content-Type": "application/json"
}
response = <http://requests.post|requests.post>(
url,
headers=headers,
auth=(username, password)
)
if response.ok:
print("Prefect flow triggered successfully.")
return response.json()
else:
print(f"Failed to trigger Prefect flow. Status: {response.status_code}, Response: {response.text}")
return None
Note the key differences:
- The path is /api/deployments/{deployment_id}/create_flow_run
(not /deployments/{id}/run
)
- The endpoint returns JSON with details about the created flow run
You can also optionally include parameters and tags in the request body:
python
# Optional: include parameters or tags
data = {
"parameters": {"key": "value"},
"tags": ["tag1", "tag2"]
}
response = <http://requests.post|requests.post>(
url,
headers=headers,
auth=(username, password),
json=data
)
The full API documentation for this endpoint can be found at: https://docs-3.prefect.io/v3/api-ref/rest-api/server/deployments/create-flow-run-from-deploymentESmith
07/08/2025, 12:55 PMMarvin
07/08/2025, 12:55 PMMarvin
07/08/2025, 12:56 PMpython
url = f"{PREFECT_API_URL}/api/deployments/{deployment_id}/create_flow_run"
2. Using the flow runs endpoint:
python
url = f"{PREFECT_API_URL}/api/flow_runs/create_deployment_flow_run"
Here's the corrected code using the second approach, which might be more reliable:
python
import requests
def trigger_prefect_flow(deployment_id):
username = "admin"
password = "password"
url = f"{PREFECT_API_URL}/api/flow_runs/create_deployment_flow_run"
headers = {
"Content-Type": "application/json"
}
# Include the deployment_id in the request body
data = {
"deployment_id": deployment_id
}
response = <http://requests.post|requests.post>(
url,
headers=headers,
auth=(username, password),
json=data
)
if response.ok:
print("Prefect flow triggered successfully.")
return response.json()
else:
print(f"Failed to trigger Prefect flow. Status: {response.status_code}, Response: {response.text}")
return None
Also, please verify:
1. Your PREFECT_API_URL
doesn't have a trailing slash
2. The deployment ID exists and is valid
3. You can access other API endpoints successfully
If you're still getting a 405 error, could you share:
1. The exact URL you're trying to hit (with sensitive info redacted)
2. Whether you can access the Prefect UI and see the deployment there
3. Whether you can trigger the deployment from the UI successfully
This will help narrow down if it's an authentication, URL structure, or API endpoint issue.