Hi <@ULVA73B9P>, how to connect prefect using self...
# ask-marvin
p
Hi @Marvin, how to connect prefect using self host api with auth?
m
thought for 265.2 seconds
Great question! Assuming you’re on Prefect 3.x, here’s how to connect to a self‑hosted Prefect API that’s protected with auth. Key point: Self-hosted Prefect Server uses HTTP Basic Auth (username:password). API keys are only for Prefect Cloud. 1) Protect the server with Basic Auth - Set a username:password on the server and start it:
Copy code
export PREFECT_SERVER_API_AUTH_STRING="admin:supersecret"
prefect server start
2) Configure clients (CLI, workers, flows) to connect with auth - Point to your server and pass the same auth string:
Copy code
export PREFECT_API_URL="<http://your-prefect-host:4200/api>"
export PREFECT_API_AUTH_STRING="admin:supersecret"
- Or set via Prefect config:
Copy code
prefect config set PREFECT_API_URL="<http://your-prefect-host:4200/api>"
prefect config set PREFECT_API_AUTH_STRING="admin:supersecret"
- Then your CLI and workers will work as usual, e.g.:
Copy code
prefect worker start --pool my-pool
3) Programmatic usage (Python) - You can rely on the env vars above or pass explicitly:
Copy code
from prefect import get_client

async def main():
    async with get_client(
        api="<http://your-prefect-host:4200/api>",
        auth_string="admin:supersecret",
    ) as client:
        await client.read_healthcheck()
Notes - If you previously used Prefect Cloud, make sure any
PREFECT_API_KEY
isn’t interfering; for self-hosted,
PREFECT_API_AUTH_STRING
is what’s used. - If your API is behind a reverse proxy, ensure it forwards the
Authorization
header to Prefect. Helpful docs: - Security settings (auth) - API client - Self-hosting Prefect If you can share how you’re connecting (CLI, worker, Python code, Kubernetes, etc.), I can tailor the exact steps and env/manifest examples for your setup.