> **For coding agents and LLMs:** This is one page from the Social Fetch docs (markdown export). For curated orientation and workflow guidance, start with [`/llms.txt`](https://www.socialfetch.dev/llms.txt); for the full endpoint list with links to pages like this one, use [`/llms-endpoints.txt`](https://www.socialfetch.dev/llms-endpoints.txt); use [`/llms.json`](https://www.socialfetch.dev/llms.json) when you need structured JSON for tool registration.

## This page

- **On-site (HTML):** [https://www.socialfetch.dev/docs/integrations/x402](https://www.socialfetch.dev/docs/integrations/x402)
- **Markdown (.mdx) URL:** [https://www.socialfetch.dev/docs/integrations/x402.mdx](https://www.socialfetch.dev/docs/integrations/x402.mdx)

## API base URL and authentication

- **API origin (from OpenAPI `servers`):** `https://api.socialfetch.dev`
- **Authentication:** send `x-api-key: sfk_...` on `/v1/**` routes unless the operation is explicitly anonymous (check OpenAPI `security`, the [API reference hub](https://www.socialfetch.dev/docs/api.mdx), [`/llms.txt`](https://www.socialfetch.dev/llms.txt), or [`/llms.json`](https://www.socialfetch.dev/llms.json) for each route).
- **OpenAPI JSON:** [https://www.socialfetch.dev/openapi.json](https://www.socialfetch.dev/openapi.json)

## Recommended docs entrypoints (this site)

- [Documentation overview](https://www.socialfetch.dev/docs.mdx) — top-level orientation (markdown).
- [Quickstart](https://www.socialfetch.dev/docs/quickstart.mdx) — authenticate with `x-api-key`, validate auth with `whoami`, and understand the JSON envelope.
- [SDK](https://www.socialfetch.dev/docs/sdk.mdx) — official TypeScript SDK guide, including `SocialFetchClient`, `Result`, and `unwrap()`.
- [SDK reference](https://www.socialfetch.dev/docs/sdk-reference.mdx) — exhaustive SDK method inventory and route mapping for agents, tooling, and power users.
- [Choose the right endpoint](https://www.socialfetch.dev/docs/choose-endpoint.mdx) — task-oriented route selection for smoke tests, profiles, list endpoints, and single-item lookups.
- [Capability matrix](https://www.socialfetch.dev/docs/capability-matrix.mdx) — fast comparison of identifiers, pagination, outcomes, media download, and SDK coverage.
- [Integrations](https://www.socialfetch.dev/docs/integrations.mdx) — MCP for AI clients, n8n verified node, Apify Store Actors, SDK, and REST API connection paths.
- [MCP integration](https://www.socialfetch.dev/docs/integrations/mcp.mdx) — hosted `/mcp` server, OAuth, Cursor/VS Code/Claude install snippets, 87 endpoint tools, plus docs_search/docs_read for implementation help.
- [n8n integration](https://www.socialfetch.dev/docs/integrations/n8n.mdx) — install `n8n-nodes-socialfetch`, credentials, and workflow examples.
- [Apify integration](https://www.socialfetch.dev/docs/integrations/apify.mdx) — Store Actors under @social-fetch, PPE billing, dataset export, and quick start.
- [`/llms-endpoints.txt`](https://www.socialfetch.dev/llms-endpoints.txt) — every documented operation with a direct link to that route's agent-readable markdown page (prefer this over parsing OpenAPI).
- [`/llms.json`](https://www.socialfetch.dev/llms.json) — structured machine-readable operation inventory with parameter names, pagination, outcomes, credits, and SDK mapping.
- [API reference hub](https://www.socialfetch.dev/docs/api.mdx) — human-friendly index of operations with links into generated pages.
- [Errors](https://www.socialfetch.dev/docs/errors.mdx) — shared error envelope and HTTP status guidance.
- [Credits](https://www.socialfetch.dev/docs/credits.mdx) — metering, `402`, and planning batch jobs.
- Outcome semantics such as `found`, `not_found`, and `private` are documented in [Errors](https://www.socialfetch.dev/docs/errors.mdx) and on operation pages when present in the OpenAPI contract.

## Markdown docs convention

- Every docs page has a markdown twin: append **`.mdx`** to the docs pathname (for example `/docs/quickstart` → `/docs/quickstart.mdx`).
- Agents that send `Accept: text/markdown` on `/docs/**` HTML URLs may receive markdown directly (same URL, `Vary: Accept`).

---
# x402 (https://www.socialfetch.dev/docs/integrations/x402)

Most integrations should use an [API key and credits](/docs/credits). **x402** is the walk-up path: call a paid `/v1` endpoint without a key, pay **USDC on Base**, get the same JSON back.

Account routes (`/v1/whoami`, `/v1/balance`) and NL Ask (`POST /v1/ask`) always need an API key — they are not on the x402 rail.

Paid endpoints are also discoverable in the [Coinbase x402 Bazaar](https://docs.cdp.coinbase.com/x402/bazaar) catalog (HTTP and MCP).

How it works

```mermaid
sequenceDiagram
  participant You
  participant API as Social Fetch API
  participant Wallet

  You->>API: Request (no API key)
  API-->>You: 402 + PAYMENT-REQUIRED
  You->>Wallet: Sign payment
  You->>API: Same request + PAYMENT-SIGNATURE
  API-->>You: 200 + data + PAYMENT-RESPONSE
```

1. Call a paid endpoint **without** `x-api-key`.
2. Read HTTP **402** and the `PAYMENT-REQUIRED` header (Base64 JSON — amount, network, pay-to, **scheme**).
3. Sign the payment (wallet or [`@x402/fetch`](https://docs.x402.org/getting-started/quickstart-for-buyers)).
4. Retry the **same** request with `PAYMENT-SIGNATURE`.
5. On success, use the JSON body as usual; `PAYMENT-RESPONSE` is the settlement receipt.

Price matches the Starter pack list rate: **$0.014 per credit**. Metering is the same as the credits rail (including attempt floors on some search endpoints).

Optional: send a [payment-identifier](https://docs.x402.org/extensions/payment-identifier) so retries stay idempotent.

Exact vs upto

The `PAYMENT-REQUIRED` challenge includes a **scheme**. Register both schemes on your buyer client so every paid route works.

| Scheme      | When you see it                                                   | Buyer setup                                                                                          |
| ----------- | ----------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| **`exact`** | Flat-price routes (most endpoints)                                | Sign each request. USDC uses EIP-3009 — **no** on-chain approve                                      |
| **`upto`**  | Metered routes (e.g. list/search that charge per returned record) | Authorize a **ceiling**; settle charges only what was used. Needs a one-time Permit2 approve (below) |

If you only register `ExactEvmScheme`, metered/`upto` calls will fail after the payment challenge.

One-time Permit2 approve

For **`upto`** on Base, your buyer wallet must approve the canonical Permit2 contract to spend USDC **once**:

* USDC (Base): `0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913`
* Permit2: `0x000000000022D473030F116dDEE9F6B43aC78BA3`

Call `USDC.approve(Permit2, maxUint256)` (or a large allowance) from the paying wallet. Exact / EIP-3009 routes do **not** need this. This is standard x402 EVM buyer setup for Permit2-based `upto`, not a Social Fetch-specific quirk. See the [CDP / x402 buyer docs](https://docs.cdp.coinbase.com/x402/quickstart-for-buyers).

Distinguish HTTP 402

| Kind           | How to tell                                                   | What to do                                                          |
| -------------- | ------------------------------------------------------------- | ------------------------------------------------------------------- |
| Credits        | `error.code = "insufficient_credits"` (API key present)       | Top up credits                                                      |
| x402 challenge | `PAYMENT-REQUIRED` header + `error.code = "payment_required"` | Pay USDC and retry                                                  |
| Settle failed  | `error.code = "payment_settlement_failed"`                    | Not a successful response — retry only with a new payment if needed |

Do **not** treat every `402` as “out of credits.” For walk-up pay, look for the `PAYMENT-REQUIRED` header. Full envelopes: [Errors](/docs/errors).

Try unpaid

```bash
curl -i "https://api.socialfetch.dev/v1/twitter/profiles/elonmusk"
```

Expect `402`, a `PAYMENT-REQUIRED` header, and a JSON body pointing at this page (and the API-key quickstart).

Pay from Node

Install the official buyer helpers, then wrap `fetch`. Register **Base** (`eip155:8453`) and **both** Exact and Upto schemes.

```bash
npm install @x402/fetch @x402/evm viem
```

```ts
import { wrapFetchWithPayment, x402Client } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { UptoEvmScheme } from "@x402/evm/upto/client";
import { privateKeyToAccount } from "viem/accounts";

const signer = privateKeyToAccount(
  process.env.EVM_PRIVATE_KEY as `0x${string}`,
);

const client = new x402Client()
  .register("eip155:8453", new ExactEvmScheme(signer))
  .register("eip155:8453", new UptoEvmScheme(signer));

const fetchWithPayment = wrapFetchWithPayment(fetch, client);

const res = await fetchWithPayment(
  "https://api.socialfetch.dev/v1/twitter/profiles/elonmusk",
);

console.log(res.status, await res.json());
console.log(res.headers.get("PAYMENT-RESPONSE"));
```

Keep the private key server-side. Prefer the official [buyer quickstart](https://docs.x402.org/getting-started/quickstart-for-buyers) for other languages (Go, Python, etc.).

MCP

[MCP](/docs/integrations/mcp) supports the same dual-rail idea:

* **OAuth** → tools bill **credits** on your account (whoami, balance, and NL Ask always need OAuth).
* **Anonymous** (no Bearer) → paid tools challenge with x402; attach payment meta and settle in USDC (Exact + Upto, same Permit2 rule as HTTP).

Invalid Bearer still returns **401** (it does not fall through to x402).

Further reading

* [x402 protocol docs](https://docs.x402.org/)
* [Buyer quickstart](https://docs.x402.org/getting-started/quickstart-for-buyers)
* [Payment identifier](https://docs.x402.org/extensions/payment-identifier)
* [Coinbase x402 Bazaar](https://docs.cdp.coinbase.com/x402/bazaar)
* [Credits & billing](/docs/credits) — API key path
* [Errors](/docs/errors) — `402` envelopes