|
| 1 | +# Workflow execution with API Key |
| 2 | + |
| 3 | +This example shows how to secure your Temporal application with API Key authentication. |
| 4 | +This is required to connect with Temporal Cloud or any production Temporal deployment that uses API Key authentication. |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | + |
| 8 | +1. A Temporal Cloud account |
| 9 | +2. A namespace in Temporal Cloud |
| 10 | +3. An API Key for your namespace |
| 11 | + |
| 12 | +## Getting your API Key |
| 13 | + |
| 14 | +1. Log in to your Temporal Cloud account |
| 15 | +2. Navigate to your namespace |
| 16 | +3. Go to Namespace Settings > API Keys |
| 17 | +4. Click "Create API Key" |
| 18 | +5. Give your API Key a name and select the appropriate permissions |
| 19 | +6. Copy the API Key value (you won't be able to see it again) |
| 20 | + |
| 21 | +## Export env variables |
| 22 | + |
| 23 | +Before running the example you need to export the following env variables: |
| 24 | + |
| 25 | +```bash |
| 26 | +# Your Temporal Cloud endpoint (e.g., us-east-1.aws.api.temporal.io:7233) |
| 27 | +export TEMPORAL_ENDPOINT="us-east-1.aws.api.temporal.io:7233" |
| 28 | + |
| 29 | +# Your Temporal Cloud namespace |
| 30 | +export TEMPORAL_NAMESPACE="your-namespace" |
| 31 | + |
| 32 | +# Your API Key from Temporal Cloud |
| 33 | +export TEMPORAL_API_KEY="your-api-key" |
| 34 | +``` |
| 35 | + |
| 36 | +## Running this sample |
| 37 | + |
| 38 | +This sample consists of two components that need to be run in separate terminals: |
| 39 | + |
| 40 | +1. First, start the worker: |
| 41 | +```bash |
| 42 | +./gradlew -q execute -PmainClass=io.temporal.samples.apikey.ApiKeyWorker |
| 43 | +``` |
| 44 | + |
| 45 | +2. Then, in a new terminal, run the starter: |
| 46 | +```bash |
| 47 | +./gradlew -q execute -PmainClass=io.temporal.samples.apikey.Starter |
| 48 | +``` |
| 49 | + |
| 50 | +## Expected result |
| 51 | + |
| 52 | +When running the worker, you should see: |
| 53 | +```text |
| 54 | +[main] INFO i.t.s.WorkflowServiceStubsImpl - Created WorkflowServiceStubs for channel: ManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=1, target=us-east-1.aws.api.temporal.io:7233}} |
| 55 | +[main] INFO io.temporal.internal.worker.Poller - start: Poller{name=Workflow Poller taskQueue="MyTaskQueue", namespace="your-namespace"} |
| 56 | +Worker started. Press Ctrl+C to exit. |
| 57 | +``` |
| 58 | + |
| 59 | +When running the starter, you should see: |
| 60 | +```text |
| 61 | +[main] INFO i.t.s.WorkflowServiceStubsImpl - Created WorkflowServiceStubs for channel: ManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=1, target=us-east-1.aws.api.temporal.io:7233}} |
| 62 | +[main] INFO io.temporal.internal.worker.Poller - start: Poller{name=Workflow Poller taskQueue="MyTaskQueue", namespace="your-namespace"} |
| 63 | +done |
| 64 | +``` |
| 65 | + |
| 66 | +## Troubleshooting |
| 67 | + |
| 68 | +If you encounter any issues: |
| 69 | + |
| 70 | +1. Verify your environment variables are set correctly: |
| 71 | + ```bash |
| 72 | + echo $TEMPORAL_ENDPOINT |
| 73 | + echo $TEMPORAL_NAMESPACE |
| 74 | + echo $TEMPORAL_API_KEY |
| 75 | + ``` |
| 76 | + |
| 77 | +2. Check that your API Key has the correct permissions for your namespace |
| 78 | + |
| 79 | +3. Ensure your namespace is active and accessible |
| 80 | + |
| 81 | +4. If you get connection errors, verify your endpoint is correct and accessible from your network |
| 82 | + |
| 83 | +5. Make sure you're running the commands from the correct directory (where the `gradlew` script is located) |
0 commit comments