<@ULVA73B9P> is there a way to authenticate to Pre...
# ask-marvin
j
@Marvin is there a way to authenticate to Prefect Cloud without running
prefect cloud login
m
thought for 172.4 seconds
n
yes, you only have to set
PREFECT_API_URL
(and
PREFECT_API_KEY
if using cloud) and that's ultimately what
prefect cloud login
does, via your profile
m
Yes. In Prefect 3.x you can authenticate non-interactively by setting settings directly—no need to run
prefect cloud login
. The most common options: - Environment variables (recommended; highest precedence) - Set both: -
PREFECT_API_URL
= https://api.prefect.cloud/api/accounts/&lt;ACCOUNT_ID&gt;/workspaces/&lt;WORKSPACE_ID> -
PREFECT_API_KEY
= your user or service account API key - Example:
Copy code
export PREFECT_API_URL="<https://api.prefect.cloud/api/accounts/<ACCOUNT_ID>/workspaces/<WORKSPACE_ID>>"
    export PREFECT_API_KEY="pnu_XXXXXXXXXXXXXXXX"
- Note: There are no separate `PREFECT_CLOUD_ACCOUNT_ID`/`WORKSPACE_ID` settings in 3.x; IDs are embedded in
PREFECT_API_URL
. You can copy IDs from the app URL: https://app.prefect.cloud/account/&lt;ACCOUNT_ID&gt;/workspace/&lt;WORKSPACE_ID&gt;/... - Profiles (persist settings without a login flow) - Create/use a profile and set values:
Copy code
prefect profile create cloud-noninteractive
    prefect profile use cloud-noninteractive
    prefect config set api.url="<https://api.prefect.cloud/api/accounts/<ACCOUNT_ID>/workspaces/<WORKSPACE_ID>>" api.key="pnu_XXXXXXXXXXXXXXXX"
- You can inspect with:
Copy code
prefect profile inspect
- Programmatic (for short-lived contexts)
Copy code
from prefect.settings import PREFECT_API_KEY, PREFECT_API_URL
  from prefect.settings.context import temporary_settings

  with temporary_settings({
      PREFECT_API_URL: "<https://api.prefect.cloud/api/accounts/<ACCOUNT_ID>/workspaces/<WORKSPACE_ID>>",
      PREFECT_API_KEY: "pnu_XXXXXXXXXXXXXXXX",
  }):
      # your Prefect code here
      pass
Workers/containers/CI - For pull-based workers (Docker/Kubernetes/VMs), set
PREFECT_API_URL
and
PREFECT_API_KEY
as container or pod env vars (ideally via secrets). This is all that’s needed to connect to your Cloud workspace. - Prefer service account API keys for non-user automation. Helpful docs: - Connect to Prefect Cloud - Settings and profiles overview - API keys - Service accounts - Manage settings If you share where you’re running (local, Docker, Kubernetes, or CI), I can give a drop-in example for that environment.
upvote 1