How to stop API keys leaking into Cursor

Keep real API keys outside the repository and limit which files and commands the coding agent can access. Replace credential values with safe examples before asking for help. If your editor feature supports a custom model endpoint, verify that it routes through a masking gateway with a fictional secret before using it on real work.

Example

To debug a missing API variable, give the agent a checked-in .env.example with PAYMENT_API_KEY=example, the code that loads it and an error that contains only the variable name. Keep the real value in your secret manager or runtime environment.

What to do

  1. Inventory credentials in local config, terminal output, fixtures and deployment files. Move real values into a secret manager or the narrowest runtime environment the application needs.
  2. Keep a safe configuration template in the repository. Apply the editor’s file exclusions and use a restricted environment for agent commands so broad terminal access does not undo those exclusions.
  3. Check whether the specific chat feature, provider and editor version support a custom base URL. Use your own provider credential upstream and a separate Spendplane gateway key in the compatible client.
  4. Make a test request containing a fake credential that matches your redaction rules. Confirm gateway activity for that request and inspect sanitised input only in your authorised test setup.
  5. Review exported logs and diffs before sharing them. If a real key has reached a model or transcript, revoke it at the provider and check usage from the earliest known exposure.

How to verify

Start with a synthetic repo and test normal chat, file mentions and agent commands separately. Record which requests reach the gateway and verify the fake credential is absent from the model-bound test payload.

Limits

Gateway masking applies to traffic routed through the gateway. Check inline completions, remote agents, terminal output and provider-managed features individually. The Free gateway allowance covers 250 compatible requests a month. Your upstream provider bills you separately for model usage on your own key.

Frequently asked questions

Is ignoring .env in Git enough?
Git exclusions help keep files out of commits. Check separately whether the editor or the agent’s terminal can read the file.
Can I use the Free plan for Cursor?
The Free plan can cover eligible requests routed through the gateway. Check base URL support for the exact feature and editor version you use. Features that send directly to the editor’s provider are outside that path.
What if I pasted a real production key into chat?
Revoke and replace it at the provider, then review usage and permissions. Handle copies in transcripts and shared links through your incident process.

Spendplane for this workflow

Set up a Spendplane gateway key

Start free

Related reading