Code Examples
TypeScript (x402-fetch)
TypeScript with x402-fetch
The simplest way to use 402claw from code. The x402-fetch library handles X402 payment negotiation automatically.
Install
npm install x402-fetchGenerate an image
import { wrapFetch } from 'x402-fetch'
const fetchX402 = wrapFetch(fetch, {
payerWalletPrivateKey: process.env.WALLET_PRIVATE_KEY,
})
// Step 1: Make the request (x402-fetch handles the 402 → sign → retry flow)
const res = await fetchX402('https://402claw.cloud/api/x402/nano-banana', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: 'A photorealistic sunset over the ocean',
aspect_ratio: '16:9',
}),
})
const { taskId, statusUrl } = await res.json()
console.log('Task created:', taskId)
// Step 2: Poll for results
let result
while (true) {
await new Promise(r => setTimeout(r, 2000))
const status = await fetch(`https://402claw.cloud${statusUrl}`).then(r =>
r.json()
)
if (status.status === 'success') {
result = status.resultUrl
break
}
if (status.status === 'failed') {
throw new Error(`Generation failed: ${status.error}`)
}
console.log(`Status: ${status.status}, elapsed: ${status.elapsed}s`)
}
console.log('Image URL:', result)With optional parameters
const res = await fetchX402('https://402claw.cloud/api/x402/nano-banana', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: 'A cyberpunk city at night',
aspect_ratio: '16:9',
output_format: 'webp',
}),
})Request Parameters
All generation endpoints accept these parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
prompt | string | Yes | Your generation prompt (1–10,000 characters) |
aspect_ratio | string | No | Image aspect ratio: 1:1 (default), 4:3, 3:4, 16:9, 9:16 |
output_format | string | No | Output format: png, jpg, webp |
resolution | string | No | Output resolution (provider-dependent) |
The prompt field is required and must be between 1 and 10,000 characters.
Last updated on