The Atlas Administration API accepts service accounts (OAuth 2.0 client credentials, the recommended method) or legacy API keys (HTTP Digest). Service account secrets are shown once and expire between 8 hours and 365 days; access tokens last 1 hour. These credentials manage Atlas itself; they do not read cluster data.
MongoDB Atlas key types
| Type | Prefix | Secret? | Scope | Expiry |
|---|---|---|---|---|
| Service account (client ID and client secret) Atlas shows the client secret only once. The client ID is not secret. | — | Yes | Roles granted in the organization and projects | Secrets expire after the duration chosen at creation, 8 hours to 365 days; access tokens last 1 hour |
| Organization API key (public and private key) Legacy method using HTTP Digest authentication. Needs an IP access list entry when the org requires one. | — | Yes | Roles granted at the organization and project level | — |
How to create a MongoDB Atlas key
- Sign in to Atlas as an Organization Owner.
- Open Applications in the Identity & Access section, then the Service Accounts tab.
- Create the service account, assign roles and generate a client secret with a chosen duration.
- Copy the secret. Atlas shows it only once.
Limiting what the key can do
Permissions come from Atlas roles, not the credential type alone. Grant the service account only the roles and projects it needs. The Admin API can also be limited by an IP access list; requests from other addresses get a 403.
How to rotate a MongoDB Atlas key
- Open the service account in Applications → Service Accounts.
- Click Generate New Client Secret, choose a duration and copy it.
- Update your application with the new client secret.
- Click Revoke on the old secret and confirm.
After you generate a new secret, the old one expires within 7 days at most. Rotating or revoking secrets needs the Organization Owner role for a project-level service account with wider access.
How to revoke a MongoDB Atlas key
- On the service account page, click Revoke on the secret and confirm.
- Revoke an issued access token with the Revoke OAuth2 Token endpoint.
- Or delete the secret with
deleteOrgServiceAccountSecretin the Admin API.
If your MongoDB Atlas key leaked
- Generate a new client secret, then revoke the exposed one immediately.
- Revoke any access tokens issued from it.
- Review Atlas activity feed and access list for changes you did not make.
Environment variables and CLI
MongoDB Atlas's SDKs and docs read the key from MONGODB_ATLAS_PUBLIC_API_KEY, MONGODB_ATLAS_PRIVATE_API_KEY, MONGODB_ATLAS_CLIENT_ID, MONGODB_ATLAS_CLIENT_SECRET or MONGODB_ATLAS_ACCESS_TOKEN.
Atlas CLI (atlas): Reads MONGODB_ATLAS_PUBLIC_API_KEY and MONGODB_ATLAS_PRIVATE_API_KEY, or service account MONGODB_ATLAS_CLIENT_ID and MONGODB_ATLAS_CLIENT_SECRET; browser login yields an access token valid for 12 hours
Things that catch people out
- MongoDB calls API keys a legacy authentication method and recommends service accounts.
- Atlas API credentials manage the platform; database access uses database users and connection strings, which are separate credentials.
- Service account access tokens last one hour and cannot be refreshed; request a new one.
- Project-scoped URLs return 401 if the credential belongs to a different organization than the project.
Recent incidents that put MongoDB Atlas keys at risk
If you were affected by one of these, rotate your MongoDB Atlas keys using the rotation steps above.
- VS Code and Open VSX extensions leaked 550+ secrets, including tokens that could publish updates 2025-10-15
Using your MongoDB Atlas key with AI agents
A coding agent that can read your .env file or your shell history can read the key, and anything in its context can end up in logs, transcripts or a prompt-injected request. The safer pattern is to keep the key in a credential broker that performs the authenticated step for the agent and hands back only the result.
BotPassword works this way. For a MongoDB Atlas key, these intents fit:
| Intent | What BotPassword does |
|---|---|
run_with | Runs a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output. |
http_request | Makes an authenticated API call after checking the destination against the hosts this key may be sent to. The response comes back with credential-shaped values redacted, and one that holds the key itself is refused. |
cli_login | Lets git, Docker, the AWS CLI and kubectl log in through bpw's credential helper, so the token is fetched when the tool needs it and not written into its config. The helper hands the value to the tool, so the grant is always disclosing. Other tools use run_with; OAuth device logins are planned. |
rotate | Partly built: creates a new key, stores it as pending, verifies it, promotes it, then revokes the old key after a grace period. Today it works for Cloudflare tokens and AWS IAM keys you have set up, and an agent can start it only under a grant with autopilot; other providers cannot be rotated through their API yet. |
store_secret | Saves a value the agent already holds as a new vault item. |
MongoDB Atlas also publishes its own MCP server. Whatever server the agent uses, keep the credential it authenticates with out of the agent's context.
Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents
Frequently asked questions
Where do I find my MongoDB Atlas API key?
Atlas recommends service accounts: open Applications → Service Accounts in the Identity & Access section. Legacy API keys are managed under organization access.
Do MongoDB Atlas service account secrets expire?
Yes. You choose a duration from 8 hours to 365 days when generating the secret.
How do I rotate a MongoDB Atlas service account secret?
Generate a new client secret, update your application, then revoke the old one. An old secret expires within 7 days of the new one's creation.
Can an AI agent use my MongoDB Atlas key without seeing it?
Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a .env file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with run_with, http_request, store_secret, so the agent gets the result and never the value.