x402 API 통합 가이드

앱이나 에이전트에 배경 제거를 통합하는 데 필요한 모든 것.

빠른 시작

이미지를 원시 본문으로 POST 요청을 전송합니다. 인증 헤더 불필요 — API는 결제에 x402를 사용합니다.

# Step 1: discovery — returns HTTP 402 with payment details
curl -X POST https://x402.clearcanvas.app/v1/remove-background \
  -H "Content-Type: image/png" \
  --data-binary @photo.png

# Step 2: paid request — attach X-PAYMENT header with signed USDC transfer
curl -X POST https://x402.clearcanvas.app/v1/remove-background \
  -H "Content-Type: image/png" \
  -H "X-PAYMENT: <base64-signed-payload>" \
  --data-binary @photo.png \
  --output result.png

x402를 통한 인증

x402는 표준 HTTP 결제 프로토콜입니다. 첫 번째 요청 시 API는 결제 정보와 함께 HTTP 402를 반환합니다. Coinbase x402 SDK를 사용하여 Base에서 USDC 전송에 서명하고 X-PAYMENT 헤더와 함께 재전송합니다.

GitHub에서 x402 SDK 보기

TypeScript

import fs from 'fs';
import { wrapFetchWithPayment } from '@coinbase/x402';
import { privateKeyToAccount } from 'viem/accounts';

const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
const fetch402 = wrapFetchWithPayment(fetch, account);

const res = await fetch402('https://x402.clearcanvas.app/v1/remove-background', {
  method: 'POST',
  headers: { 'Content-Type': 'image/png' },
  body: fs.readFileSync('photo.png'),
});

const buffer = await res.arrayBuffer();
fs.writeFileSync('result.png', Buffer.from(buffer));

Python

import os
import httpx
from x402.client import wrap_httpx   # pip install x402

client = wrap_httpx(private_key=os.environ["PRIVATE_KEY"])
resp = client.post(
    "https://x402.clearcanvas.app/v1/remove-background",
    content=open("photo.png", "rb").read(),
    headers={"Content-Type": "image/png"},
)
open("result.png", "wb").write(resp.content)

요청 형식

  • 엔드포인트
  • Body: 원시 이미지 바이트 (PNG, JPEG 또는 WebP)
  • Content-Type: image/png | image/jpeg | image/webp
  • 선택 사항: WebP 출력을 위한 ?format=webp

응답 형식

  • HTTP 200 — 바이너리 이미지 (PNG 또는 WebP)
  • HTTP 402 — 결제 필요 (body에 x402 세부 정보)

오류 처리

  • 400 — 유효하지 않은 이미지 (잘못된 형식, 매직 바이트 불일치, 또는 크기 초과)
  • 413 — 파일이 너무 큼 (> 25 MB)
  • 415 — 지원하지 않는 Content-Type
  • 429 — 요청 한도 초과 (미결제 요청: 30회/분/IP)
  • 500 — 처리 실패 (retryable: true)
  • 503 — 프로세서 일시적 사용 불가 (retryable: true)

제한 및 형식

  • 최대 파일 크기: 25 MB
  • 최대 해상도: 10,000 × 10,000 px
  • 지원 입력: PNG, JPEG, WebP
  • 출력: PNG (기본값) 또는 WebP

네트워크

API는 Base 메인넷에서 실행됩니다. 결제는 실제 USDC로 처리됩니다 (이미지당 $0.05). 요청하기 전에 지갑에 Base의 USDC가 있는지 확인하세요.

통합할 준비가 되셨나요?