# API keys

Canonical URL: https://developers.iagenify.com/sdk/api-keys/
Language: en

A key starts with ia_ and is handed over exactly once. The SDK exchanges it for a session, caches that session, and never sends the key again.

Status: Authorization header only · exchanged once · bound to the session cache

## Where a key comes from {#where}

You create one in your account, signed in, and it is shown to you once. Then you hand it to your program — through its environment, or through your secret store. Your program reads a key; it never issues one.

> **The SDK cannot make a key, on purpose**
>
> A program able to mint keys would turn one leaked key into as many as its holder wants, each surviving whatever you revoke. Issuing a credential is something a person does, not something a credential does for itself.

What the SDK can do is see what exists and stop what should not — enough to audit and to cut off, which is what a running program legitimately needs.

```javascript
const { keys } = await api.keys.list();
// [{ id, name, prefix, scopes, lastUsedAt, expiresAt, isActive, createdAt }]

// Which key is this process running on? Match on the prefix you deployed with it —
// never on the key itself, which is not in the list and should not be compared.
const mine = keys.find((k) => k.prefix === process.env.IAGENIFY_KEY_PREFIX);

// And stop one, by the id that only list() carries.
await api.keys.revoke(someKey.id);
```

> **The raw key exists once**
>
> A lost key is replaced, never recovered. Revoking is immediate: the hash stops matching on the next request.

## What the SDK does with it {#exchange}

On the first call the key is POSTed to `/api/auth/sessions/exchange`, together with the runtime, the framework, a machine id and detected resources. What comes back is a JWT and a refresh token, cached for every later call.

```javascript
// What the exchange sends
{ apiKey, storageId?, storageType: "filesystem" | "localStorage" | "memory",
  framework: "node" | "next.js" | "gatsby" | "vite" | "browser",
  appName, machineId, environment: { os, arch, hostname, runtime }, resources }

// What every later request carries
Authorization: Bearer <jwt>
X-IaGenify-Machine-Id: <sha256 of hostname|os|arch|runtime>
X-IaGenify-Resources: {"cpu":{...},"memory":{...},"disk":[...]}
X-IaGenify-Version: 0.2.2
```

When the JWT expires the refresh token renews it. When refreshing fails, the key is exchanged again. On a `401` the SDK drops only the JWT and retries once, so a signing change does not break a long-running process.

> **Concurrent exchanges are deduplicated**
>
> Keyed by apiKey plus appName, across instances. React StrictMode's double render produces one exchange, not two — and two exchanges would create two sessions.

## Changing the key {#fingerprint}

The cached session carries a fingerprint of the key it came from. Hand the SDK a different key and the cache is not reused.

> **Two keys never share a cached session**
>
> Swapping the key swaps the identity on the very next call, with no stale session in between. You do not have to clear anything yourself.

## Several keys on one machine {#several}

The session file is per path. Give each account its own, or each one overwrites the last.

```javascript
const tenantA = new IaGenify({
  apiKey: keyA,
  sessionPath: "/var/lib/app/sessions/tenant-a",
  appName: "billing-worker",
});

const tenantB = new IaGenify({
  apiKey: keyB,
  sessionPath: "/var/lib/app/sessions/tenant-b",
  appName: "billing-worker",
});
```

## Calling the API without the SDK {#raw}

The API also accepts a raw key. It must be the `Authorization` header: the cookie and the `?api_key=` query parameter were removed, because a key in a URL ends up in logs, referrers and browser history.

```bash
curl https://api.iagenify.com/api/auth/me \
  -H "Authorization: ApiKey ia_your_key_here"
```

## Where to keep it {#storing}

- Server-side, in an environment variable.
- Never in client-side code. A key in a browser bundle is a key you have published.
- In a browser app, call the SDK from your own server and pass the results down.
- In Node the session lands in `~/.iagenify/session`, in a directory created with mode `0700`.
- In CI, one key per pipeline, named, so revoking one does not stop the others.

```python
import os

keys = api.keys.list()["keys"]
# [{"id", "name", "prefix", "scopes", "lastUsedAt", "expiresAt", "isActive", "createdAt"}]

# Which key is this process running on? Match on the prefix you deployed with it —
# never on the key itself, which is not in the list and should not be compared.
mine = next((k for k in keys if k["prefix"] == os.environ["IAGENIFY_KEY_PREFIX"]), None)

# And stop one, by the id that only list() carries.
api.keys.revoke(some_key["id"])
```

```python
# What the exchange sends
{"apiKey": ..., "storageId": ..., "storageType": "filesystem",
 "framework": "python", "appName": ..., "machineId": ...,
 "environment": {"os": ..., "arch": ..., "hostname": ..., "runtime": ...}, "resources": ...}

# What every later request carries
# Authorization: Bearer <jwt>
# X-IaGenify-Machine-Id: <sha256 of hostname|os|arch|runtime>
# X-IaGenify-Resources: {"cpu":{...},"memory":{...},"disk":[...]}
```

```python
tenant_a = IaGenify(
    api_key=key_a,
    session_path="/var/lib/app/sessions/tenant-a",
    app_name="billing-worker",
)

tenant_b = IaGenify(
    api_key=key_b,
    session_path="/var/lib/app/sessions/tenant-b",
    app_name="billing-worker",
)
```
