Temporal Client - Python SDK
This guide introduces Temporal Clients. It explains the role and use of Clients and shows you how to configure your Python Client code to connect to the Temporal Service.
The pages shows how to do the following:
- Connect to a local development Temporal Service
- Connect to Temporal Cloud
- Start a Workflow Execution
Connect to development Temporal Service
How to connect to the local Temporal CLI development Temporal Service using the Python SDK
A Temporal Client enables you to communicate with the Temporal Service. Communication with a Temporal Service includes, but isn't limited to, the following:
- Starting Workflow Executions.
- Sending Signals to Workflow Executions.
- Sending Queries to Workflow Executions.
- Getting the results of a Workflow Execution.
- Providing an Activity Task Token.
A Temporal Client cannot be initialized and used inside a Workflow. However, it is acceptable and common to use a Temporal Client inside an Activity to communicate with a Temporal Service.
When you are running a Temporal Service locally (such as the Temporal CLI), the number of connection options you must provide is minimal.
Many SDKs default to the local host or IP address and port that Temporalite and Docker Compose serve (127.0.0.1:7233
).
Use the connect()
method on the Client class to create and connect to a Temporal Client to the Temporal Service.
View the source code
in the context of the rest of the application code.
# ...
async def main():
client = await Client.connect("localhost:7233")
result = await client.execute_workflow(
YourWorkflow.run,
"your name",
id="your-workflow-id",
task_queue="your-task-queue",
)
print(f"Result: {result}")
if __name__ == "__main__":
asyncio.run(main())
Connect a Temporal Client to a Temporal Service
How to connect to a Temporal Service
A Temporal Client enables you to communicate with the Temporal Service. Communication with a Temporal Service includes, but isn't limited to, the following:
- Starting Workflow Executions.
- Sending Signals to Workflow Executions.
- Sending Queries to Workflow Executions.
- Getting the results of a Workflow Execution.
- Providing an Activity Task Token.
A Temporal Client cannot be initialized and used inside a Workflow. However, it is acceptable and common to use a Temporal Client inside an Activity to communicate with a Temporal Service.
When you are running a Temporal Service locally (such as the Temporal CLI), the number of connection options you must provide is minimal.
Many SDKs default to the local host or IP address and port that Temporalite and Docker Compose serve (127.0.0.1:7233
).
Use the connect()
method on the Client class to create and connect to a Temporal Client to the Temporal Service.
View the source code
in the context of the rest of the application code.
# ...
async def main():
client = await Client.connect("localhost:7233")
result = await client.execute_workflow(
YourWorkflow.run,
"your name",
id="your-workflow-id",
task_queue="your-task-queue",
)
print(f"Result: {result}")
if __name__ == "__main__":
asyncio.run(main())
Connect to Temporal Cloud
How to connect to Temporal Cloud using an API key
To use an API key with the Temporal Python SDK, you will need to provide additional connection options:
- Your API Key value
- Your Namespace and Account id combination, which follows the format
<namespace_id>.<account_id>
. - The endpoint may vary. The most common endpoint used is the gRPC regional endpoint, which follows the format:
<region>.<cloud_provider>.api.temporal.io:7233
. - For Namespaces with High Availability features with API key authentication enabled, use the gRPC Namespace endpoint:
<namespace>.<account>.tmprl.cloud:7233
. This allows automated failover without needing to switch endpoints.
You can find the Namespace and Account ID, as well as the endpoint, on the Namespaces tab:
Now, when instantiating a Temporal client
in your Temporal Python SDK code, provide the API key:
client = await Client.connect(
<endpoint>,
namespace=<namespace_id>.<account_id>,
api_key=<APIKey>,
tls=True,
)
To update an API key, replace the api_key
parameter:
client.api_key = my_key_updated
How to connect to Temporal Cloud using TLS
When you connect to Temporal Cloud, you need to provide additional connection and client options that include the following:
- The Temporal Cloud Namespace Id.
A Namespace Id is made up of a Namespace name appended by your unique five- or six-digit Temporal Cloud Account Id.
You can find this Account Id in the URL of your Namespace and on the "Namespaces" page on the Temporal Cloud website.
For example, in
https://cloud.temporal.io/namespaces/yournamespacename.a2fx6/
, your Account Id isa2fx6
. The fully qualified Namespace Id for your client with this Namespace isyournamespacename.a2fx6
. - The Namespace's gRPC endpoint. An endpoint listing is available at the Temporal Cloud Website on each Namespace detail page. The endpoint contains the Namespace Id and port.
- mTLS CA certificate.
- mTLS private key.
For more information about managing and generating client certificates for Temporal Cloud, see How to manage certificates in Temporal Cloud.
For more information about configuring TLS to secure inter- and intra-network communication for a Temporal Service, see Temporal Customization Samples.
Use the connect()
method on the Client class to create and connect to a Temporal Client to the Temporal Service.
Then specify the TLSConfig arguments to connect to a Temporal Service with TLS enabled.
The client_cert
must be combined with client_private_key
to authenticate the Client.
View the source code
in the context of the rest of the application code.
from temporalio.client import Client, TLSConfig
# ...
# ...
async def main():
with open("client-cert.pem", "rb") as f:
client_cert = f.read()
with open("client-private-key.pem", "rb") as f:
client_private_key = f.read()
client = await Client.connect(
"your-custom-namespace.tmprl.cloud:7233",
namespace="<your-custom-namespace>.<account-id>",
tls=TLSConfig(
client_cert=client_cert,
client_private_key=client_private_key,
# domain=domain, # TLS domain
# server_root_ca_cert=server_root_ca_cert, # ROOT CA to validate the server cert
),
)