Vugar Yagubov
07/07/2025, 7:43 PMprefect-client
with a new API URL and API key? Can the API URL and API key be set from within Python code?Marvin
07/07/2025, 7:43 PMMarvin
07/07/2025, 7:45 PMPrefectClient
class. Here's how to do it:
python
from prefect.client.orchestration import PrefectClient
# Create a new client with custom settings
client = PrefectClient(
api_key="your-api-key", # Optional: Prefect Cloud API key
api="<http://your-api-url>" # Optional: API URL for your Prefect server
)
A few important points:
1. The client is designed to be immutable - instead of modifying an existing client, you create a new one with the desired settings.
2. If you don't specify parameters, the client will use values from your Prefect settings:
- API URL defaults to what's in your PREFECT_API_URL
setting
- API key defaults to what's in your PREFECT_API_KEY
setting
3. You can also modify these settings using environment variables:
python
import os
os.environ["PREFECT_API_URL"] = "<http://your-api-url>"
os.environ["PREFECT_API_KEY"] = "your-api-key"
# Any new client created after this will use these settings by default
client = PrefectClient()
4. For Prefect Server (self-hosted), you typically only need to set the API URL.
For Prefect Cloud, you'll need both the API URL and an API key.
This approach gives you flexibility to work with multiple Prefect instances or switch between them programmatically. Each PrefectClient
instance maintains its own configuration, so you can have multiple clients with different settings active simultaneously if needed.
Remember that changes to the client configuration only affect new client instances - existing clients will maintain their original configuration until they are recreated.Vugar Yagubov
07/07/2025, 7:56 PMpython
from prefect.client.orchestration import PrefectClient
# Create a new client with custom settings
client = PrefectClient(
api_key="your-api-key", # Optional: Prefect Cloud API key
api="<http://your-api-url>" # Optional: API URL for your Prefect server
)
then how should I modify my code the way that it will use the new client:
flow = await Flow.afrom_source(
source=gitlab_repo,
entrypoint=deployment_entry.entrypoint,
)
await flow.deploy(
**deployment_dict,
version=new_hash,
)
??
Here there is no client
in the code. looks like Flow uses by default some client.Marvin
07/07/2025, 7:56 PMMarvin
07/07/2025, 7:57 PMclient_injector
context manager. Here's how to modify your code:
python
from prefect.client.orchestration import PrefectClient
from prefect.client.utilities import client_injector
# Create your custom client
custom_client = PrefectClient(
api_key="your-api-key",
api="<http://your-api-url>"
)
# Use the client_injector context manager
async with client_injector(custom_client):
flow = await Flow.afrom_source(
source=gitlab_repo,
entrypoint=deployment_entry.entrypoint,
)
await flow.deploy(
**deployment_dict,
version=new_hash,
)
The client_injector
context manager ensures that your custom client is used for all Prefect operations within its scope. This includes both the Flow.afrom_source()
and flow.deploy()
operations.
A few important points:
1. The context manager must be used in an async context (with async with
)
2. All operations that need to use the custom client should be within the context manager's scope
3. Once you exit the context manager, Prefect will revert to using the default client configuration
This approach allows you to temporarily override the default client settings for specific operations while maintaining the ability to use different client configurations in different parts of your code.Vugar Yagubov
07/07/2025, 8:09 PMpython
from prefect.client.orchestration import PrefectClient
from prefect.client.utilities import client_injector
# Create your custom client
custom_client = PrefectClient(
api_key="your-api-key",
api="<http://your-api-url>"
)
# Use the client_injector context manager
async with client_injector(custom_client):
together with serve operation?:
klass.flow_method.serve(
name=deployment_name,
paused=klass.paused,
triggers=klass.triggers,
parameters=klass.parameters,
description=klass.description,
tags=klass.tags,
version=klass.version,
)
asking, because here serve operation is not asyncMarvin
07/07/2025, 8:09 PMMarvin
07/07/2025, 8:09 PMserve
operation, you'll need to use a synchronous client instead. Here's how to modify your code:
python
from prefect.client.orchestration import get_client
# Create a synchronous client
custom_client = get_client(
api_key="your-api-key",
api="<http://your-api-url>",
sync_client=True # This is important - it creates a synchronous client
)
# Set the client before the serve operation
with custom_client:
klass.flow_method.serve(
name=deployment_name,
paused=klass.paused,
triggers=klass.triggers,
parameters=klass.parameters,
description=klass.description,
tags=klass.tags,
version=klass.version,
)
Key points:
1. Use get_client()
with sync_client=True
instead of PrefectClient
directly
2. The synchronous client can be used with a regular with
statement (not async with
)
3. The client context manager will ensure all operations within its scope use your custom client configuration
If you need to mix both async and sync operations, you can create both types of clients:
python
from prefect.client.orchestration import PrefectClient, get_client
# For async operations
async_client = PrefectClient(
api_key="your-api-key",
api="<http://your-api-url>"
)
# For sync operations
sync_client = get_client(
api_key="your-api-key",
api="<http://your-api-url>",
sync_client=True
)
# Use async client for async operations
async with client_injector(async_client):
# Your async operations here
pass
# Use sync client for sync operations
with sync_client:
klass.flow_method.serve(
name=deployment_name,
paused=klass.paused,
triggers=klass.triggers,
parameters=klass.parameters,
description=klass.description,
tags=klass.tags,
version=klass.version,
)
This approach gives you the flexibility to handle both synchronous and asynchronous operations while maintaining custom client settings for both contexts.Vugar Yagubov
07/09/2025, 10:23 PMAuthorization: Bearer __token__
. But when I check headers of websocket requests, such header is missing.
Could you tell me why and suggest how to make it appear?Marvin
07/09/2025, 10:23 PMMarvin
07/09/2025, 10:25 PMAuthorization
header, WebSocket connections in Prefect typically authenticate using query parameters in the WebSocket URL.
Here's why and how to handle it:
1. HTTP Requests: These use the standard Authorization: Bearer <token>
header format, which you're correctly seeing.
2. WebSocket Connections: These authenticate using a token query parameter in the WebSocket URL. The authentication token should be automatically included in the WebSocket connection URL when you properly configure your API key.
To ensure proper authentication for WebSocket connections:
1. Make sure you've set your API key correctly:
bash
export PREFECT_API_KEY="your-api-key"
2. If you're creating a custom client, ensure you pass the API key:
python
from prefect.client import get_client
client = get_client(api_key="your-api-key")
3. If you're running a self-hosted server, verify your authentication settings in your server configuration.
If you're still not seeing proper authentication after these steps, you might want to:
1. Verify your API key is being properly recognized by running a simple authenticated HTTP request
2. Check your server logs for any authentication-related errors
3. Ensure you're not behind a proxy that might be stripping authentication parameters
For more details on Prefect security settings and authentication, you can refer to the security settings documentation.