# Get an Invariant Earth API key (for AI agents)

Use this when the person you work for needs an Invariant Earth API key and you are running in their terminal,
editor or chat. They approve once from an email; you poll and receive the key. API base: `https://api.invariant-ai.com`.

## Rules

- If `INVARIANT_API_KEY` is already set in the environment, or is in the project's git-ignored `.env` or secret
  store, use it and stop here. Check without printing it.
- Ask the person for their email. Do not guess it or take it from git config without asking.
- Never print, log or commit the key. Never put it in chat, a URL, or a tracked file.

## Step 1: start

```bash
curl -s -X POST https://api.invariant-ai.com/v1/agent-signup \
  -H "Content-Type: application/json" \
  -d '{"email": "pat@example.com", "key_name": "Research bot", "client_name": "Claude Code"}'
```

`email` is required. `key_name` labels the key; `client_name` is shown to the person so they know who is asking.

```json
{
  "claim_token": "clm_…",
  "user_code": "042917",
  "verification_uri": "https://api.invariant-ai.com/approve?v=…",
  "expires_in": 600,
  "interval": 5
}
```

Keep `claim_token` secret: it is what collects the key. Then tell the person, with the code exactly as returned:

> I asked Invariant for an API key for pat@example.com. Open the email from Invariant ("Approve an Invariant API
> key for Claude Code"), check it shows the code **042917**, and click Review and approve.
> You can also approve with Google here: https://api.invariant-ai.com/approve?v=…

## Step 2: poll

```bash
curl -s -X POST https://api.invariant-ai.com/v1/agent-signup/token \
  -H "Content-Type: application/json" \
  -d '{"claim_token": "clm_…"}'
```

Poll every `interval` seconds until the status is final.

| Response | What to do |
|---|---|
| `{"status": "pending"}` | Wait `interval` seconds and poll again |
| `{"status": "slow_down"}` | Add 5 seconds to the interval and poll again |
| `{"status": "expired"}` | The 10 minutes ran out. Offer to start again |
| `{"status": "denied"}` | The person declined. Stop and tell them |
| `{"status": "approved", "api_key": "ief_…", …}` | Store the key (step 3) and stop polling |
| `400 invalid_request` | Unknown token, or the key was already handed over. Do not retry |
| `429 rate_limited` with `reason: "agent_signups"` | Too many sign-ups from this network or open for this email. Wait `Retry-After` seconds |

The approved response is returned **once**:

```json
{
  "status": "approved",
  "api_key": "ief_…",
  "key_id": "key_1767f11e60d5",
  "key_name": "Research bot",
  "email": "pat@example.com",
  "dashboard_url": "https://docs.invariant-ai.com/get-api-key"
}
```

## Step 3: store the key

Make sure `.env` is in `.gitignore` (add it first if not), then write `INVARIANT_API_KEY=<api_key>` to `.env`, or
to the project's secret store, without echoing it. Tell the person the key name and that they can revoke it any time
with `DELETE /v1/keys/me`.

Send it on every request as the `X-API-Key` header:

```bash
curl -s https://api.invariant-ai.com/v1/fetch \
  -H "X-API-Key: $INVARIANT_API_KEY" -H "Content-Type: application/json" \
  -d '{"lat": 39.0438, "lng": -77.4874, "fields": ["elevation"]}'
```

## Without an agent loop

A script can use the emailed code instead: `POST /v1/keys/verify-email` with `{"email": …}`, ask the person for the
6-digit code from the email, then `POST /v1/keys` with `{"email": …, "code": "042917", "name": …}`.

## Next

- Quickstart: https://docs.invariant-ai.com/quickstart.md
- Every field: https://api.invariant-ai.com/v1/meta/fields
- MCP server (Claude, ChatGPT, Cursor, Codex): https://docs.invariant-ai.com/mcp/installation.md
