This example shows how to secure your Temporal application with API Key authentication. This is required to connect with Temporal Cloud or any production Temporal deployment that uses API Key authentication.
- A Temporal Cloud account
- A namespace in Temporal Cloud
- An API Key for your namespace
- Log in to your Temporal Cloud account
- Navigate to your namespace
- Go to Namespace Settings > API Keys
- Click "Create API Key"
- Give your API Key a name and select the appropriate permissions
- Copy the API Key value (you won't be able to see it again)
Before running the example you need to export the following env variables:
# Your Temporal Cloud endpoint (e.g., us-east-1.aws.api.temporal.io:7233)
export TEMPORAL_ENDPOINT="us-east-1.aws.api.temporal.io:7233"
# Your Temporal Cloud namespace
export TEMPORAL_NAMESPACE="your-namespace"
# Your API Key from Temporal Cloud
export TEMPORAL_API_KEY="your-api-key"This sample consists of two components that need to be run in separate terminals:
- First, start the worker:
./gradlew -q execute -PmainClass=io.temporal.samples.apikey.ApiKeyWorker- Then, in a new terminal, run the starter:
./gradlew -q execute -PmainClass=io.temporal.samples.apikey.StarterWhen running the worker, you should see:
[main] INFO i.t.s.WorkflowServiceStubsImpl - Created WorkflowServiceStubs for channel: ManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=1, target=us-east-1.aws.api.temporal.io:7233}}
[main] INFO io.temporal.internal.worker.Poller - start: Poller{name=Workflow Poller taskQueue="MyTaskQueue", namespace="your-namespace"}
Worker started. Press Ctrl+C to exit.
When running the starter, you should see:
[main] INFO i.t.s.WorkflowServiceStubsImpl - Created WorkflowServiceStubs for channel: ManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=1, target=us-east-1.aws.api.temporal.io:7233}}
[main] INFO io.temporal.internal.worker.Poller - start: Poller{name=Workflow Poller taskQueue="MyTaskQueue", namespace="your-namespace"}
done
If you encounter any issues:
-
Verify your environment variables are set correctly:
echo $TEMPORAL_ENDPOINT echo $TEMPORAL_NAMESPACE echo $TEMPORAL_API_KEY
-
Check that your API Key has the correct permissions for your namespace
-
Ensure your namespace is active and accessible
-
If you get connection errors, verify your endpoint is correct and accessible from your network
-
Make sure you're running the commands from the correct directory (where the
gradlewscript is located)