# Server-side access

Canonical URL: https://ai.marketcheck.com/ai/docs/server-side-access/

> Call reports from your own backend with a Desk key or OAuth client credentials. No browser, no human in the loop.

Everything here works from a backend service with no human present. If you are wiring reports into your own dealership platform, DMS, or internal tool, this is the page you need.

## Two ways to authenticate

AI Desk issues two kinds of machine credential. Both belong to your team, spend the team balance, and carry the scopes and spend caps you set:

- **Desk keys**: a static key sent as a bearer token. The simplest option for scripts, scheduled jobs and CLI agents: one header, no token exchange.
- **OAuth client credentials**: a client id and a client secret your server holds, exchanged for a 15-minute token. For backends that would rather hold a secret and send only short-lived tokens.

If you already hold a MarketCheck API key for the data endpoints, note that it is a different system and does not work here. AI Desk is a separate account with a separate balance.

One thing to rule out early: the REST endpoint the web app uses to run a report authenticates off a browser session cookie, so it cannot be called server to server. The MCP endpoint below is the supported path, and despite the name it is an ordinary JSON over HTTPS POST. No AI client is involved.

## Desk keys

Create a key under [Keys](https://ai.marketcheck.com/account/keys) (owners and billing managers). Choose which reports and data tools it may run and, optionally, per-call, daily and monthly caps. The secret is shown once; your team's Default key can also be revealed again later.

Send it on every call, with no token exchange:

```bash
curl -X POST https://ai.marketcheck.com/mcp \
  -H "Authorization: Bearer mck_live_..." \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"get_account_status","arguments":{}}}'
```

From there, the request and response shapes in steps 3 and 4 below are the same.

- A key is backed by an agent, so it carries the same scopes and caps, and its runs show under its name in your agents' activity.
- Rotate a key to replace its secret: the old one stops working immediately. Revoke it and it stops on the next call.
- A key does not expire. Keep it in your secret store, never in code or a committed file.
- A CLI agent can get a key for its user without any copying: see the device flow in [Getting started](https://ai.marketcheck.com/ai/docs/getting-started/).

## OAuth client credentials

The steps below cover client credentials, the standard machine-to-machine grant. Use it when you want short-lived tokens rather than a long-lived key.

## Step 1: create an agent

An **agent** is a machine identity that belongs to your team and spends your team's balance under limits you set.

Go to [Agents](https://ai.marketcheck.com/account/agents) and create one. You choose:

| Setting | What it does |
|---|---|
| **Name** | Required. Shown in your list of agents |
| **Spend limits: Per call** | Refuses any single call priced above this |
| **Spend limits: Per day** | Total spend per day |
| **Spend limits: Per month** | Total spend per calendar month |
| **Webhook URL** | Optional. Receives `report.completed` and `wallet.debited` events |
| **Reports this agent can run** | The agent's scopes. Grant only what it needs. At least one is required |

Set the caps. An unattended service that loops is the realistic failure mode, and a monthly cap is the cheapest insurance against it.

You are shown a **client id and a client secret**. The secret is shown once. Store it the way you store any other production credential.

## Step 2: get a token

```bash
curl -X POST https://ai.marketcheck.com/oauth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=client_credentials" \
  -d "client_id=YOUR_CLIENT_ID" \
  -d "client_secret=YOUR_CLIENT_SECRET" \
  -d "scope=reports:window-sticker"
```

```json
{
  "access_token": "eyJhbGciOi...",
  "token_type": "Bearer",
  "expires_in": 900,
  "scope": "reports:window-sticker"
}
```

Credentials may go in the body as shown, or in an HTTP Basic authorization header. Either is accepted.

Three things to build around:

- **The token lives 15 minutes** (`expires_in: 900`). Fetch one per batch of work rather than one per call.
- **There is no refresh token.** That is deliberate: a short life means revoking an agent takes effect quickly. Request a new token when the old one expires.
- **Scope is an intersection, never a widening.** Ask for a scope the agent was not granted and you simply do not get it. Omit `scope` entirely and you get everything the agent has.

## Step 3: run a report

```bash
curl -X POST https://ai.marketcheck.com/mcp \
  -H "Authorization: Bearer eyJhbGciOi..." \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "generate_window_sticker",
      "arguments": { "vin": "1GNSKBKC5MR123456" }
    }
  }'
```

The tool name and its arguments are listed on each report's page in the catalog.

## Step 4: read the response

```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "content": [{ "type": "text", "text": "{ ...report JSON as a string... }" }]
  }
}
```

**`content[0].text` is a JSON string, not an object.** Parse it. This is the single detail that costs integrators the most time.

Parsed, it contains:

```json
{
  "data": {
    "executive_summary": { "headline": "...", "verdict": "...", "key_metrics": [] },
    "window_sticker": { "pdf_url": "https://ai.marketcheck.com/api/window-sticker/9f2c...", "format": "PDF (11×17 landscape)" },
    "vehicle": { "vin": "..." },
    "hosted_report": { "url": "https://ai.marketcheck.com/r/..." }
  },
  "billing": {
    "list_price_usd": 1.99,
    "charged_usd": 1.99,
    "balance_after_usd": 248.05,
    "report": "Window Sticker"
  }
}
```

Every run reports its own cost and the balance left. Log `balance_after_usd` and alert on it, so a stalled balance surfaces before it stops your service.

## Step 5: fetch artifacts

Reports that produce a file return a URL rather than bytes.

```bash
curl -H "Accept: application/pdf" "PDF_URL" -o sticker.pdf
```

Adding `?format=pdf` does the same thing. Open the same URL in a browser instead and it renders in a viewer with download and print controls.

Artifact and hosted-report links are **unguessable but public**. Anyone holding the link can open it. Treat them like share links: fine to email to a customer, not a substitute for access control.

## Errors

| Response | Meaning | What to do |
|---|---|---|
| `invalid_client` | Unknown client id, wrong secret, or the agent is revoked | Check the credential. Re-issue if revoked |
| `401` with a `WWW-Authenticate` header ending `error="invalid_token"` | The token expired or is not valid, its agent was suspended or revoked, or a Desk key was rotated or revoked | Fetch a new token, or use the key's current secret |
| `401` with a `WWW-Authenticate` header and no `error` | No token was sent, on a method other than `tools/call` | Send the token as `Authorization: Bearer <token>` |
| `isError` result whose text has `"status": "payment_required"` | A `tools/call` sent with no token is taken as a pay-per-call purchase with no account. The report did not run and nothing was charged | Send the token as `Authorization: Bearer <token>` and call again. Ignore the checkout in the response |
| Result text says your connector was not granted access to the report | The token or Desk key does not carry that report's scope. Scopes are fixed when a token is issued, so a report added to the agent later is not in a token you already hold. A Desk key reads its scopes on every call | Add the report to the agent (its **Reports** button on the Agents page), then fetch a new token. For a Desk key, add it under **What this key can run** on the Keys page; the next call runs with the same key |
| Result text says access to the report is turned off for this connection | The account owner restricted reports under **Account-wide report access** on the Connections page, and this one is not allowed | The owner allows it there. The next call runs, with no new token |
| Result text names a balance below the price | Not enough balance | Top up, or enable auto top-up |
| `isError` with a failure message | The run failed | **Nothing was charged.** Safe to retry |

A failed run is never billed. This matters for retry logic: you can retry without worrying about double charges.

## Keeping an unattended service running

**Enable auto top-up.** A service that stops at a zero balance at 2am is the most common way this integration fails. Set a threshold and an amount so the balance refills itself.

**Watch your caps.** A daily or monthly cap doing its job looks identical to a broken integration. Alert on cap-refusal responses so you can tell the two apart.

**Revocation is immediate.** Revoking an agent or a Desk key takes effect on the next call, not when a current token expires, because the status is re-checked on every request.
