# Code examples for text, image, and speech

Every client uses the same official service address and payment lifecycle. The payment wrapper must use official x402 packages and locally enforce the payment network, asset, recipient, and spending limits in atomic United States Dollar Coin (USDC) units. Request bodies differ by capability; use the generated guide for the selected endpoint.

## TypeScript

Construct `paymentFetch` with the official `@x402/core`, `@x402/evm`, and `@x402/fetch` client packages. Then pass it to the repository wrapper:

```ts
import { OnchainRouterClient } from '@onchain-router/client';

const router = new OnchainRouterClient({
  baseUrl: process.env.ONCHAIN_ROUTER_URL!,
  paymentFetch,
});

const response = await router.chat({
  model: 'gemini-3.6-flash',
  messages: [{ role: 'user', content: 'Explain why the sky appears blue.' }],
  max_tokens: 1024,
  stream: false,
});

const body = await response.json();
console.log(body.choices[0].message.content);
console.log(response.headers.get('x-receipt-id'));
```

The local payment policy must reject any network other than Base mainnet, an incorrect USDC contract, a changed recipient, an unsupported payment scheme, or a maximum above the caller's spending limit.

## Python

The maintained repository example accepts an `httpx.Client` that is already wrapped by the official x402 software development kit for Python:

```python
result = paid_chat(payment_client)
print(result["choices"][0]["message"]["content"])
```

Do not implement Ethereum Improvement Proposal 712 (EIP-712), Permit2, or payment settlement code in your application. Use the official libraries.

## Direct Hypertext Transfer Protocol request

An ordinary unpaid request is useful for inspecting the challenge:

```bash
curl -i "$ONCHAIN_ROUTER_URL/v1/chat/completions" \
  -H "content-type: application/json" \
  -H "x-idempotency-key: $(uuidgen)" \
  --data '{"model":"gemini-3.6-flash","messages":[{"role":"user","content":"Say hello."}],"max_tokens":512,"stream":false}'
```

Expect Hypertext Transfer Protocol (HTTP) status 402. Do not construct the paid retry by hand. Pass the response to an official x402 buyer client.

## Other capabilities

Use the same payment-aware client for every paid route:

- [Messages](https://llm.agenticfi.wtf/docs/api/messages) accepts the Anthropic-compatible message shape.
- [Image Generations](https://llm.agenticfi.wtf/docs/api/image-generations) accepts an image model, prompt, size, aspect ratio, and response format.
- [Text to Speech](https://llm.agenticfi.wtf/docs/api/audio-speech) accepts text, model, voice, MP3 response format, and optional speed.
- [Speech to Text](https://llm.agenticfi.wtf/docs/api/audio-transcriptions) accepts one MP3 either as canonical Base64 JSON or as multipart form data.

Do not assume that a text-specific wrapper method supports media. Send the exact body shown by OpenAPI through the same official payment-aware HTTP client.
