Configuration & CLI Gateway Config

OpenClaw Gateway Token: Secure Your Agent Network the Right Way

The gateway token is a single secret that controls every agent connection and every API call in your OpenClaw system. One misconfiguration exposes everything. Here's the exact setup that keeps production systems secure.

MK
M. Kim
AI Product Specialist
Feb 21, 2025 14 min read 7.1k views
Updated Feb 21, 2025
Key Takeaways
  • The gateway token is the single credential that authenticates all agent clients and REST API callers — treat it like a root password
  • Generate tokens with at least 32 random bytes — use openssl rand -hex 32 or Python's secrets.token_hex(32)
  • Reference it via environment variable in gateway.yaml — never hardcode the raw secret string
  • Use the dual-token rotation method to swap tokens in production without any downtime
  • Rotate immediately if the token is ever exposed — check gateway logs for unauthorized access in the exposure window

Every team that ships an OpenClaw setup to production eventually hits the same moment: they realize their gateway token is sitting in a .env file committed to a private repo, shared in Slack, or printed in a Docker log. 73% of credential exposure incidents involve secrets in source control. The gateway token needs a proper home from day one. Here's exactly how to set it up.

What the Gateway Token Controls

The OpenClaw gateway token is a shared secret that authenticates two categories of connection: agent clients registering their channels with the gateway, and REST API clients calling the /api/v1/ endpoints. Every agent in your network uses this token when it starts up. Every external system that calls the API uses this token in its Authorization: Bearer header.

This means a leaked gateway token gives an attacker the ability to send messages to any agent in your system, read all shared memory, query conversation history, and trigger any task your agents can perform. The blast radius is your entire agent network.

Here's what most builders get wrong: they treat the gateway token like an application password — something that rarely changes and gets shared across environments. The correct mental model is a root database credential. It needs rotation, it needs secret management, and it needs monitoring.

⚠️
One Token, Total Access

The gateway token grants unrestricted access to all agents, all channels, and all shared memory. There are no granular scopes or per-agent permissions on the token itself. Treat it accordingly: store it in a secret manager, audit its usage, and rotate it on a regular schedule.

Generating a Secure Gateway Token

The token must be cryptographically random. Dictionary words, dates, UUIDs, and short strings are not acceptable — they're either guessable or brute-forceable given enough time. Use your OS or a proper cryptographic library.

# Option 1: OpenSSL (Linux/macOS/WSL)
openssl rand -hex 32
# Output: a3f8c2e1d94b7a6f2e0c8b3d5a1f9e4c2b7d0a8e6f3c1d9b4a7e2f0c5b8d3a6

# Option 2: Python (any platform)
python3 -c "import secrets; print(secrets.token_hex(32))"

# Option 3: Node.js
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

All three methods produce a 64-character hex string — 32 bytes of entropy. This is the minimum. For high-security deployments, use 48 or 64 bytes. There's no practical upper limit on token length; the gateway doesn't truncate it.

Store the generated token in your secret manager immediately. Don't paste it anywhere else first. The clipboard is a shared resource and clipboard history tools are notoriously indiscriminate about what they capture.

Configuring the Token in gateway.yaml

Never put the raw token string directly in gateway.yaml. Use an environment variable reference. OpenClaw's gateway supports ${VAR_NAME} syntax anywhere in the config file for exactly this reason.

# gateway.yaml — correct production approach
gateway:
  token: ${OPENCLAW_GATEWAY_TOKEN}
  port: 8080
  host: "0.0.0.0"

# Set the environment variable in your deployment
# Linux/macOS (shell):
# export OPENCLAW_GATEWAY_TOKEN="a3f8c2e1d94b7a6f..."

# Docker Compose:
# environment:
#   - OPENCLAW_GATEWAY_TOKEN=${OPENCLAW_GATEWAY_TOKEN}

# Kubernetes Secret:
# env:
#   - name: OPENCLAW_GATEWAY_TOKEN
#     valueFrom:
#       secretKeyRef:
#         name: openclaw-secrets
#         key: gateway-token

The gateway reads environment variables at startup. If the referenced variable is missing, the gateway refuses to start — it logs an error and exits rather than running without a token. This fail-safe behavior is intentional: a gateway with no authentication is worse than a gateway that won't start.

💡
Use a Secret Manager, Not a .env File

AWS Secrets Manager, HashiCorp Vault, GCP Secret Manager, and Azure Key Vault all support injecting secrets as environment variables into your runtime. This is the correct production pattern. A .env file is acceptable for local development only — never commit it, and add it to .gitignore immediately.

Token Rotation Without Downtime

OpenClaw supports simultaneous dual-token authentication for zero-downtime rotation. Here's the exact sequence that works without interrupting any connected agents.

# Step 1: Add the new token as a secondary in gateway.yaml
gateway:
  token: ${OPENCLAW_GATEWAY_TOKEN}           # old token
  token_secondary: ${OPENCLAW_GATEWAY_TOKEN_NEW}  # new token
  port: 8080

# Step 2: Deploy the updated gateway config
# Both tokens now work simultaneously

# Step 3: Update all agent configs to use the new token
# agents/researcher/agent.yaml:
# gateway_token: ${OPENCLAW_GATEWAY_TOKEN_NEW}

# Step 4: Update all API integrations and restart them

# Step 5: Remove token_secondary from gateway.yaml and redeploy
gateway:
  token: ${OPENCLAW_GATEWAY_TOKEN_NEW}   # new token only
  port: 8080

The window between Step 2 and Step 5 is when both tokens are valid. Keep this window short — hours, not days. During this period, the old token is still live. Complete Step 5 as soon as all agents and API clients are confirmed working with the new token.

Here's where most people stop — but don't skip this step. After completing the rotation, revoke the old token from whatever secret manager you use. A revoked token in the manager is your guarantee it won't be inadvertently restored by a deployment that pulls an older secret version.

Common Gateway Token Mistakes

  • Hardcoding the token in gateway.yaml — the config file ends up in version control, gets copied to staging environments, and gets included in Docker images. Use environment variable references exclusively.
  • Using the same token across environments — dev, staging, and production should each have distinct tokens. A dev token leak shouldn't threaten production. Keep them separate in your secret manager.
  • Never rotating the token — tokens that never change accumulate exposure risk over time: team members leave, integrations get decommissioned, and old tokens persist in config backups. Rotate quarterly at minimum.
  • Logging the token accidentally — some deployment scripts echo environment variables for debugging. Add OPENCLAW_GATEWAY_TOKEN to your log scrubbing rules and double-check CI/CD log output after any pipeline change.
  • Skipping the exposure audit on rotation — when you rotate because of a suspected exposure, check gateway logs for unauthorized API calls or unexpected agent registrations. Rotation stops future abuse; the audit tells you whether past abuse happened.

Frequently Asked Questions

What is the OpenClaw gateway token?

The gateway token is a shared secret authenticating all connections to your OpenClaw gateway — both agent clients registering their channels and API clients calling REST endpoints. It acts as a master credential for your agent network and must be treated with the same care as a database root password.

How do I generate a secure gateway token?

Use openssl rand -hex 32 on Linux/macOS or Python's secrets.token_hex(32). These produce 32 bytes of cryptographic randomness — the minimum recommended length. Store the result in a secret manager immediately. Never use dictionary words, dates, or UUID-style strings.

Where does the gateway token go in gateway.yaml?

Use an environment variable reference: token: ${OPENCLAW_GATEWAY_TOKEN}. Set the variable in your deployment environment or secret manager. Never hardcode the raw string — it will end up in version control, Docker image layers, or deployment logs eventually.

How do I rotate the gateway token without downtime?

Add the new token as token_secondary in gateway.yaml and deploy. Both tokens work simultaneously. Update all agents and API clients to use the new token, then remove token_secondary and redeploy. Zero downtime, complete rotation.

Can I use different tokens for different agents?

Not with the base gateway token — it's a single shared secret. For browser-based clients, the browser relay session token system provides per-session isolation. For server-side agents, use network-level controls like firewall rules or VPC restrictions to limit which hosts can reach the gateway.

What should I do if my gateway token is exposed?

Rotate immediately — update gateway.yaml with a new token and restart the gateway. Update all agents and API integrations. Then audit gateway logs for unauthorized requests in the exposure window. Revoke the old token from your secret manager to prevent accidental restoration.

MK
M. Kim
AI Product Specialist

M. Kim specializes in production-hardening AI agent deployments, with particular focus on credential management, secret rotation workflows, and zero-downtime configuration changes. Has guided dozens of teams through their first production OpenClaw deployments.

Security Configuration Guides

Weekly OpenClaw security and config tips, free.