# Use with AI agents

> Everything an agent needs to find this API, read the docs and call /v1/fetch on its own.

Source: https://docs.invariant-ai.com/agents

The API was built with agents as first-class callers. Responses are plain JSON, every value carries the URL it came from, and a missing value is an explicit `null` with a reason, so a model never has to guess whether a gap is real.

## Docs an agent can read

| What | URL |
| --- | --- |
| Index of every page, for LLMs | [`/llms.txt`](https://docs.invariant-ai.com/llms.txt) |
| All docs in one Markdown file | [`/llms-full.txt`](https://docs.invariant-ai.com/llms-full.txt) |
| Any page as Markdown | Add `.md` to the URL, for example [`/quickstart.md`](https://docs.invariant-ai.com/quickstart.md). The home page is [`/index.md`](https://docs.invariant-ai.com/index.md). |
| Markdown by content negotiation | Send `Accept: text/markdown` to any page URL. |
| Live field catalog, JSON, no key | `GET https://api.invariant-ai.com/v1/meta/fields` |

Pointing a coding agent at the API? Paste `https://docs.invariant-ai.com/llms-full.txt` into its context. That file holds the whole reference.

## Rules for agents

1. Get a key with `POST /v1/keys` and the user's real email address. Ask the user for it; never invent one. The key is shown once, so store it (an environment variable such as `INVARIANT_API_KEY` is the convention).
2. Call `GET /v1/meta/fields` to learn which field names exist. It needs no key.
3. Call `POST /v1/fetch` with `lat`, `lng` (WGS84 decimal degrees, US only) and a `fields` array. Send the key in `X-API-Key`. Use a client timeout of 30 seconds or more.
4. Read `status` on each field. `ok` means use `value`. `absent` means the source has no data at that point, which is a real answer. `failed` means the source did not answer: retry later if `partial_failures[].retryable` is `true`, and do not report it as "no data".
5. When you tell a person a value, give the `source`, the `source_url` and the `dataset_vintage` with it. That lets them check the number against the agency.
6. One coordinate per request. For many points, send one request per point.

## Tool definition for Claude

Pass this in `tools` on the Anthropic Messages API:

```json
{
  "name": "invariant_fetch",
  "description": "Get cited public data for one US coordinate from the Invariant Earth API. Returns any of 220 fields (parcels, power and grid, hazards, terrain, climate, land cover, built environment; list them with GET /v1/meta/fields), each with its source URL, dataset vintage and confidence. US only: lat 18 to 72, lng -180 to -65.",
  "input_schema": {
    "type": "object",
    "properties": {
      "lat": {
        "type": "number",
        "minimum": 18,
        "maximum": 72,
        "description": "Latitude, WGS84 decimal degrees"
      },
      "lng": {
        "type": "number",
        "minimum": -180,
        "maximum": -65,
        "description": "Longitude, WGS84 decimal degrees"
      },
      "fields": {
        "type": "array",
        "items": {
          "type": "string"
        },
        "minItems": 1,
        "maxItems": 50,
        "description": "Field names from GET /v1/meta/fields, for example [\"elevation\"]"
      }
    },
    "required": [
      "lat",
      "lng",
      "fields"
    ]
  }
}
```

## Tool definition for OpenAI

The same tool for function calling with the OpenAI Chat Completions API:

```json
{
  "type": "function",
  "function": {
    "name": "invariant_fetch",
    "description": "Get cited public data for one US coordinate from the Invariant Earth API. Returns any of 220 fields (parcels, power and grid, hazards, terrain, climate, land cover, built environment; list them with GET /v1/meta/fields), each with its source URL, dataset vintage and confidence. US only.",
    "parameters": {
      "type": "object",
      "properties": {
        "lat": {
          "type": "number",
          "description": "Latitude, WGS84 decimal degrees, 18 to 72"
        },
        "lng": {
          "type": "number",
          "description": "Longitude, WGS84 decimal degrees, -180 to -65"
        },
        "fields": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "Field names, for example [\"elevation\"]"
        }
      },
      "required": [
        "lat",
        "lng",
        "fields"
      ]
    }
  }
}
```

## Run the tool

Whatever model asks for the call, your code makes it the same way:

```python
import os, requests

API = "https://api.invariant-ai.com"

def invariant_fetch(lat: float, lng: float, fields: list[str]) -> dict:
    r = requests.post(
        f"{API}/v1/fetch",
        headers={"X-API-Key": os.environ["INVARIANT_API_KEY"]},
        json={"lat": lat, "lng": lng, "fields": fields},
        timeout=30,
    )
    if r.status_code != 200:
        # 400 and 401 carry a stable code in detail.error; hand it back to the model
        return {"error": r.json().get("detail", r.text)}
    return r.json()
```

Return the whole JSON to the model. It needs `status`, `source_url` and `partial_failures` to answer honestly.

## A system prompt line that works

```text
When you need ground elevation for a US location, call invariant_fetch with
fields ["elevation"]. Report the value in meters with its source_url and
dataset_vintage. If status is "failed", say the source did not answer and
retry later; if "absent", say USGS has no elevation at that point.
```
