# MongoDB Atlas API keys: find, store, rotate, and use them safely with AI agents

Source: https://botpassword.com/keys/mongodb-atlas
Last checked: 2026-10-25

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.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| Service account (client ID and client 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) | — | Yes | Roles granted at the organization and project level | — |

## How to create a MongoDB Atlas key

1. Sign in to Atlas as an Organization Owner.
2. Open **Applications** in the Identity & Access section, then the **Service Accounts** tab.
3. Create the service account, assign roles and generate a client secret with a chosen duration.
4. 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

1. Open the service account in **Applications → Service Accounts**.
2. Click **Generate New Client Secret**, choose a duration and copy it.
3. Update your application with the new client secret.
4. Click **Revoke** on the old secret and confirm.

## How to revoke a MongoDB Atlas key

1. On the service account page, click **Revoke** on the secret and confirm.
2. Revoke an issued access token with the Revoke OAuth2 Token endpoint.
3. Or delete the secret with `deleteOrgServiceAccountSecret` in the Admin API.

## If the key leaked

1. Generate a new client secret, then revoke the exposed one immediately.
2. Revoke any access tokens issued from it.
3. Review Atlas activity feed and access list for changes you did not make.

## Environment variables

- `MONGODB_ATLAS_PUBLIC_API_KEY`
- `MONGODB_ATLAS_PRIVATE_API_KEY`
- `MONGODB_ATLAS_CLIENT_ID`
- `MONGODB_ATLAS_CLIENT_SECRET`
- `MONGODB_ATLAS_ACCESS_TOKEN`

## Gotchas

- 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.

## Using this key with AI agents (BotPassword)

- `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.

## FAQ

### 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.

## Sources

- https://www.mongodb.com/docs/atlas/configure-api-access/
- https://www.mongodb.com/docs/atlas/api/service-accounts-overview/
- https://www.mongodb.com/docs/atlas/tutorial/rotate-service-account-secrets/
- https://www.mongodb.com/docs/atlas/cli/upcoming/atlas-cli-env-variables/
- https://www.mongodb.com/docs/mcp-server/
