← All problems and solutions

How do I give an AI agent email access without handing over my API key?

Let the agent authenticate through an OAuth consent flow so the provider issues it its own credential, limited in scope, expiring and revocable. If only keys are supported, create a dedicated key for that agent, load it from an environment variable or secret store rather than the chat, and know in advance where you revoke it.

By AISend. Updated . Also as markdown.

Why it happens

The easy way to give an agent access is to paste your API key into the prompt or the client config. That key usually has the same rights as you: it can send from every domain on the account, read every email, and often create more keys. Anything pasted into a conversation also lands in transcripts, logs and sometimes the model provider's history, and a model that is tricked by injected text in an email or web page can be asked to repeat it.

The key is also shared. If one key serves your app, your scripts and the agent, you cannot revoke the agent without breaking the rest, and you cannot tell from the logs which of them made a call. Shared fixed credentials have a second cost on team plans: Anthropic's help page notes that when a connector uses an API key, everyone who uses it reaches the service with that credential's access.

You can recognise the risky setup quickly. If the key appears in a chat, a committed config file or a client setting you pasted in plain text, or if the same key is used by anything besides the agent, you are in this case.

How to give access with a smaller blast radius (any provider)

  1. Decide the minimum the agent needs. Write down the actions: send from one domain, read delivery status, nothing else. That list decides which credential type you can accept. An agent that only needs to read status should not hold a key that can send.
  2. Prefer OAuth consent when the server supports it. The MCP authorization specification says HTTP-based transports should follow its OAuth-based flow, in which the client discovers the authorization server, registers itself, and sends you to a consent page. You review what is requested, approve, and the server issues the client its own token. Read the consent screen: it should name the client, the account, and what the agent will be able to do.
  3. If you must use a key, create a dedicated one. Make a new key named for the agent and purpose, such as support-agent-prod, with the narrowest rights the provider offers. Never reuse the key your application uses. One key per agent means one revoke per agent.
  4. Load the key from the environment, not the conversation. The MCP specification says stdio servers should take credentials from the environment. Cursor supports ${env:NAME} interpolation in mcp.json headers, Claude Code takes --env for local servers, and Gemini CLI redacts environment variables matching patterns like KEY and TOKEN from the base environment unless you list them in the server's env block. Use those mechanisms and never ask an agent to "remember" the key.
    # Claude Code, local server
    claude mcp add email --env EMAIL_API_KEY="$EMAIL_API_KEY" -- npx -y some-email-mcp
    
    # Cursor mcp.json (remote)
    "headers": { "Authorization": "Bearer ${env:EMAIL_API_KEY}" }
    
    # Gemini CLI settings.json
    "env": { "EMAIL_API_KEY": "$EMAIL_API_KEY" }
  5. Put a ceiling under the credential. Use provider-side limits that a compromised agent cannot lift: a daily send cap, a verified-domain requirement, a rate limit. Combine with client-side approval prompts for send actions. A credential that can only send 100 messages a day is a bounded incident, not an open one.
  6. Make it expire and schedule the rotation. Prefer credentials that expire by themselves. For long-lived keys, put the rotation date in a calendar and rotate after any suspicion. If you use OAuth, check whether the server issues refresh tokens: OpenAI's help page warns that without them ChatGPT may lose access when the original authorization expires and users need to sign in again.
  7. Test the revoke path before you need it. Revoke the agent's credential on purpose once and confirm the next call fails with 401. Then reconnect. If you cannot find the revoke button within a minute, fix that now. Also note that removing a connector in a client does not always revoke the token at the provider.

Which credential to give an agent

A comparison of the common ways to give an agent access. These are general properties of each approach, not claims about one provider.

ApproachScoped to this agentExpires by itselfRevoke without touching other appsAppears in the chat transcript
Your main API key pasted into the chatNoNoNoYes
Your main key in an environment variableNoNoNoNo
A dedicated key for the agent, in an environment variableYesOnly if you rotate itYesNo
OAuth consent, token issued to the clientYesDepends on the serverYesNo

Whichever row you choose, the provider's own limits (daily caps, verified domains) still apply and are your last line of defence.

When to use AISend

AISend's hosted MCP server at https://api.aisend.app/mcp supports the OAuth route. A client that supports it is sent to aisend.app, where you sign in and see a consent screen that names the client, the account and the redirect host, and has Approve and Deny buttons. Approving creates an API key named "MCP OAuth: <client>" that works only on the MCP endpoint, expires after 90 days, is limited to 100 emails a day, and can be revoked on the API Keys page of the dashboard; revoking it makes the next call fail with 401. The agent can see its own key names and prefixes with list_api_keys but not the full secret, and the hosted server cannot create keys.

Cursor is the exception: AISend's OAuth sign-in does not accept Cursor's redirect, so Cursor uses an API key as a Bearer header. A key you create yourself has your account's full API access, no 100-a-day cap (it has the plan's daily cap) and does not expire until you revoke it, so make one used only for Cursor and keep it in an environment variable. The local server's create_api_key tool prints the new key in the conversation log, which is the opposite of what this page recommends; create keys in the dashboard instead.

  • If you need finer scopes than send and read (for example send-only keys), note that a key you create in AISend has full API access; confirm your provider's key options, or isolate the agent on its own account.
  • If your agent runs in CI or on a server with no human to approve, a dedicated key in a secret manager is the practical choice, and OAuth is not.
  • Sending to other people needs a paid plan and a verified domain; 10 free test emails go to your own address first; see pricing.

Copy-paste prompt for an AI assistant connected to AISend

Connect an assistant first (setup for ChatGPT, Claude, Cursor and Gemini CLI), then paste:

Assign to AISend: call list_api_keys and show me each key's name, prefix and whether it is revoked, so I can see which keys exist for the assistant connections. Then call list_domains and tell me which sender domains are verified. Do not send anything and do not ask me for an API key. Show me what you did so I can approve any next step, and tell me where in the dashboard I revoke a connection.

Frequently asked questions

Is OAuth always safer than an API key?

Not automatically. It is safer when the issued token is narrower than your key, expires, and is revocable on its own. Read the consent screen and the provider's description of what the token can do.

Can the agent read my API key from an environment variable?

A local server process can, and an agent with a shell tool could print it. Keep keys in the server's own environment, not in a shell the agent controls, and use a dedicated, capped key.

What do I do if a key was pasted into a chat?

Treat it as exposed: revoke it at the provider, create a new one, and put the new one in an environment variable. Deleting the message does not remove it from logs.

How do I revoke an AISend assistant connection?

Open the API Keys page of your AISend dashboard and revoke the key named "MCP OAuth: <client>". The assistant's next call fails with 401, and the connection also expires 90 days after it was created.

Sources

Related