How to Secure Your API Keys for Agent Use
Three credential paths, a storage hierarchy, and honest threat modeling. No "enterprise-grade" theater.
The problem
Your agent needs API keys to do useful work. Send an email through Resend, charge a card through Stripe, create a ticket in Linear — every capability requires a credential.
The default behavior is terrifying: paste the key into a config file, hope nobody reads it.
Most credential advice is written for human developers deploying web apps. Agents are different. They run unattended, make autonomous decisions about which keys to use, and often operate across multiple services in a single workflow. The attack surface is wider and the human oversight is narrower.
This guide covers what actually works.
Three credential paths
There's no single right answer. The right approach depends on how much you trust the infrastructure, how much operational control you want, and whether provider control or governed execution is the point.
Bring Your Own Key (BYOK)
You pass your existing API key to the tool or agent framework. The key stays under your control.
# Environment variable (recommended for servers/CI)
export STRIPE_API_KEY=sk_live_...
# MCP server config (Claude Desktop example)
{
"mcpServers": {
"rhumb": {
"command": "npx",
"args": ["rhumb-mcp@latest"],
"env": {
"RHUMB_API_KEY": "rhumb_live_..."
}
}
}
}
When to use it: You already have accounts with the services you need. You want full control over credentials and billing. You're comfortable managing key rotation.
Security posture: The key is only as safe as where you store it. OS keychain > environment variable > config file > hardcoded string.
Managed capabilities
Rhumb holds the service credential and delivers the capability. You never touch the underlying API key.
# You call Rhumb. Rhumb calls the service.
curl -X POST https://api.rhumb.dev/v1/capabilities/email.send/execute \
-H "X-Rhumb-Key: rhumb_live_..." \
-d '{"to": "user@example.com", "subject": "Hello"}'
When to use it: You don't want to manage individual service accounts. You want Rhumb to handle authentication, rate limits, and failover. You want per-call pricing with route, health, and cost checks before execution.
Security posture: You trust Rhumb with the service credential. You don't need to store or rotate individual service keys. Your governed API key is the only credential to protect.
Agent Vault
Rhumb stores or brokers the provider credential for you and injects it at call time, so the workflow can still touch your own systems without spraying raw provider secrets across every agent runtime.
When to use it: You need provider control or access to your own systems of record, but you want a tighter trust boundary than raw BYOK everywhere. This is the middle path between fully managed Rhumb rails and handing every runtime a live upstream key. Security posture: The agent still uses your systems, but the raw provider secret stays behind the vault boundary. You reduce copy-paste exposure and make rotation or revocation more surgical.Credential format is not the authority boundary
OAuth versus API key is the visible choice. For agents, it is not the deepest security decision. The deeper question is whether the credential maps to a containment principal the operator can reason about before the model touches production state.
Ask these questions before calling a credential strategy safe:
Treat OAuth as ceremony, API keys as bearer power, and vaults as custody. None of them replace scoped execution policy, narrow discovery, typed denials, and trace evidence.
x402 is a payment path, not a credential mode
No account signup on this rail. Payment proof authorizes the request. For repeat traffic, default to governed API key or wallet-prefund on X-Rhumb-Key, and bring BYOK or Agent Vault only when provider control is the point.
# First call: get payment instructions
curl -X POST https://api.rhumb.dev/v1/capabilities/email.send/execute
# Returns 402 with USDC payment details
# After paying on-chain:
curl -X POST https://api.rhumb.dev/v1/capabilities/email.send/execute \
-H 'X-Payment: {"tx_hash":"0x...","network":"base","wallet_address":"0x..."}'
When to use it: Your agent has a crypto wallet and you want zero-signup, request-level payment authorization. Use this when per-call payment is the point, not as the default repeat-throughput path.
Security posture: No credential to steal. The wallet's private key is the only secret, and it stays on the agent's machine. Replay protection via unique transaction hashes.
Credential storage hierarchy
Not all storage is equal. Here's the ranking from most to least secure:
| Storage | Security | When to use |
|---|---|---|
| OS keychain (macOS Keychain, Windows Credential Manager) | ★★★★★ | Desktop agents, local development |
| Secret manager (1Password, HashiCorp Vault, AWS Secrets Manager) | ★★★★★ | Teams, production, enterprise |
| Environment variables | ★★★★ | Servers, CI/CD, containers |
| Encrypted config file | ★★★ | Offline agents, air-gapped systems |
| Plaintext config file | ★★ | Last resort. Temporary only. |
| Hardcoded in source | ★ | Never. Not even in private repos. |
OS keychain example (macOS)
# Store
security add-generic-password -a "rhumb" -s "RHUMB_API_KEY" -w "rhumb_live_..."
# Retrieve at runtime
RHUMB_API_KEY=$(security find-generic-password -a "rhumb" -s "RHUMB_API_KEY" -w)
Environment variable example (Docker)
# Don't bake keys into images. Pass at runtime.
docker run -e RHUMB_API_KEY=rhumb_live_... my-agent
What we protect against (and what we don't)
Honest security requires honest threat modeling.
Rhumb protects against:- Credential exposure in logs and prompts — keys are injected at the HTTP layer, never surfaced in agent conversation history
- Replay attacks on x402 payments — unique transaction hash constraint
- Rate limit abuse per wallet (x402) or per API key (registered)
- Accidental credential leakage in tool outputs — response sanitization strips auth headers
- A compromised machine — if an attacker has root access, all credentials on that machine are exposed regardless of storage method
- Malicious agent code — if the agent framework itself is compromised, it can read any credential the agent has access to
- Social engineering — if someone tricks you into sharing your API key, that's outside Rhumb's control
We don't use phrases like "enterprise-grade security" or "bank-level encryption" because they don't mean anything specific. We tell you exactly what the boundaries are.
Best practices checklist
.env files with .gitignore, or better yet, use a secret manager reference.{
"rhumb_api_key": "op://Shared/Rhumb/credential"
}
The bottom line
The safest credential is the one that doesn't exist. x402 is the no-credential payment rail when per-call payment is the point. Managed capabilities reduce your credential surface to a single governed API key. Agent Vault is the middle path when you need provider control without exposing raw provider secrets everywhere. BYOK gives you full control when you need it.
Pick the path that matches your trust model. Store credentials in the highest-security option available. Be specific about what you're protecting against.
Your agents are only as secure as their weakest credential.
Turn the credential plan into one bounded execution path
Once the trust boundary is clear, the next move is not spraying more raw provider keys across more runtimes. Start with capability-first onboarding to choose the right access path, or open the managed lane if one governed key is the honest fit.
Credential shape changes more than setup friction. It changes what the agent can safely assume, retry, and recover from once the call is live.
Salesforce API Autopsy
Governance is strong, but auth ceremony, sandbox splits, and metadata complexity raise the real operator cost.
Self-provisioned keysTwilio API Autopsy
A cleaner key story reduces credential drag, but carrier constraints and external side effects still need explicit handling.
Permanent tokensShopify API Autopsy
Permanent access helps, but query-cost budgets and version churn still define whether unattended execution stays safe.
Once the question moves past where to store a key, the real operator work is credential lifecycle, remote authority separation, and shared-fleet containment. These three guides pick up exactly there.
API Credentials in Autonomous Agent Fleets
Covers rotation blindness, expiry handling, revocation, and shared-key cascade prevention once agents are running unattended.
Remote authority splitRemote MCP Auth: Identity vs Authority
Shows why a valid auth ceremony still is not the real boundary if discovery scope, backend principals, and denial semantics stay broad after connection.
Shared budget controlDesigning Agent Fleets That Survive Rate Limits
Shows why safer key handling is still not enough if retries, concurrency, and 429 recovery are left to chance across a fleet.