Skip to content

API Reference

Complete HTTP API documentation for the x402 Gateway.

Base URL

https://llmogevm.opengradient.ai

Authentication Flow

The x402 protocol uses a two-step authentication flow:

  1. Initial Request: Make a request without payment headers
  2. Payment Response: Server responds with 402 Payment Required and payment details
  3. Authenticated Request: Resubmit with signed payment in X-PAYMENT header

Endpoints

Chat Completions

Generate chat completions with conversational context.

http
POST /v1/chat/completions

Request Headers

HeaderRequiredDescription
Content-TypeYesMust be application/json
X-PAYMENTConditionalSigned payment payload (required after 402 response)
X-SETTLENoSettlement mode: SETTLE_INDIVIDUAL, SETTLE_INDIVIDUAL_WITH_METADATA, or SETTLE_BATCH

Request Body

json
{
  "model": "openai/gpt-4o",
  "messages": [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"}
  ],
  "max_tokens": 100,
  "temperature": 0.7,
  "stop": ["\n"],
  "tools": [],
  "tool_choice": "auto"
}
FieldTypeRequiredDescription
modelstringYesModel identifier (see Supported Models)
messagesarrayYesArray of message objects
max_tokensintegerNoMaximum tokens to generate (default: 100)
temperaturefloatNoSampling temperature 0-2 (default: 1.0)
stoparrayNoStop sequences
toolsarrayNoTool/function definitions
tool_choicestringNoTool selection mode: auto, none, or specific tool

Message Object

json
{
  "role": "user",
  "content": "Hello!"
}
FieldTypeDescription
rolestringsystem, user, assistant, or tool
contentstringMessage content
namestringOptional name for the participant
tool_callsarrayTool calls made by assistant (for assistant messages)
tool_call_idstringID of tool call being responded to (for tool messages)

Text Completions

Generate completions from a text prompt.

http
POST /v1/completions

Request Body

json
{
  "model": "openai/gpt-4o",
  "prompt": "Translate to French: Hello, how are you?",
  "max_tokens": 100,
  "temperature": 0.7,
  "stop": ["\n"]
}
FieldTypeRequiredDescription
modelstringYesModel identifier
promptstringYesText prompt
max_tokensintegerNoMaximum tokens to generate
temperaturefloatNoSampling temperature 0-2
stoparrayNoStop sequences

Payment Protocol

Step 1: Receive Payment Requirements

When you make a request without payment, the server responds with 402 Payment Required:

http
HTTP/1.1 402 Payment Required
Content-Type: application/json
X-PAYMENT-REQUIRED: <base64-encoded-payment-requirements>

Decode the X-PAYMENT-REQUIRED header to get payment details:

json
{
  "scheme": "exact",
  "network": "opengradient-10744",
  "maxAmountRequired": "1000000",
  "resource": "https://llmogevm.opengradient.ai/v1/chat/completions",
  "description": "LLM inference payment",
  "mimeType": "application/json",
  "payTo": "0x339c7de83d1a62edafbaac186382ee76584d294f",
  "maxTimeoutSeconds": 300,
  "asset": "0x...",
  "extra": {
    "name": "OUSDC",
    "version": "1"
  }
}

Step 2: Create Payment Payload

Create a payment payload matching the EIP-3009 transferWithAuthorization format:

json
{
  "from": "0xYourWalletAddress",
  "to": "0x339c7de83d1a62edafbaac186382ee76584d294f",
  "value": "1000000",
  "validAfter": 0,
  "validBefore": 1704067200,
  "nonce": "0x1234567890abcdef..."
}

Step 3: Sign the Payment

Sign the payment payload using EIP-712 typed data signing:

Domain:

json
{
  "name": "OUSDC",
  "version": "1",
  "chainId": 10744,
  "verifyingContract": "0x..."
}

Types:

json
{
  "TransferWithAuthorization": [
    {"name": "from", "type": "address"},
    {"name": "to", "type": "address"},
    {"name": "value", "type": "uint256"},
    {"name": "validAfter", "type": "uint256"},
    {"name": "validBefore", "type": "uint256"},
    {"name": "nonce", "type": "bytes32"}
  ]
}

Step 4: Submit Payment

Base64-encode the payment object and include it in the X-PAYMENT header:

json
{
  "payload": {
    "signature": "0xabcd...",
    "authorization": {
      "from": "0xYourWalletAddress",
      "to": "0x339c7de83d1a62edafbaac186382ee76584d294f",
      "value": "1000000",
      "validAfter": 0,
      "validBefore": 1704067200,
      "nonce": "0x1234567890abcdef..."
    }
  }
}
http
POST /v1/chat/completions
X-PAYMENT: eyJwYXlsb2FkIjp7...base64-encoded...
Content-Type: application/json

{...request body...}

Response Format

Successful Response

http
HTTP/1.1 200 OK
Content-Type: application/json
X-PAYMENT-RESPONSE: <base64-encoded-payment-receipt>

Chat Completion Response

json
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1704067200,
  "model": "openai/gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 8,
    "total_tokens": 18
  }
}

Text Completion Response

json
{
  "id": "cmpl-123",
  "object": "text_completion",
  "created": 1704067200,
  "model": "openai/gpt-4o",
  "choices": [
    {
      "index": 0,
      "text": "Bonjour, comment allez-vous?",
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 8,
    "completion_tokens": 6,
    "total_tokens": 14
  }
}

Payment Receipt

Decode the X-PAYMENT-RESPONSE header to get payment settlement details:

json
{
  "success": true,
  "network": "opengradient-10744",
  "chainId": 10744,
  "txHash": "0x5678...",
  "payer": "0xYourWalletAddress"
}

Error Responses

402 Payment Required

Payment needed to proceed. See Payment Protocol.

400 Bad Request

json
{
  "error": {
    "message": "Invalid request body",
    "type": "invalid_request_error",
    "code": "invalid_request"
  }
}

401 Unauthorized

json
{
  "error": {
    "message": "Invalid payment signature",
    "type": "authentication_error",
    "code": "invalid_signature"
  }
}

402 Insufficient Funds

json
{
  "error": {
    "message": "Insufficient OUSDC balance",
    "type": "payment_error",
    "code": "insufficient_funds"
  }
}

429 Rate Limited

json
{
  "error": {
    "message": "Rate limit exceeded",
    "type": "rate_limit_error",
    "code": "rate_limited"
  }
}

500 Internal Error

json
{
  "error": {
    "message": "Internal server error",
    "type": "server_error",
    "code": "internal_error"
  }
}

Tools / Function Calling

Define tools that the model can call:

json
{
  "model": "openai/gpt-4o",
  "messages": [
    {"role": "user", "content": "What's the weather in San Francisco?"}
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Get current weather for a location",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "City name"
            }
          },
          "required": ["location"]
        }
      }
    }
  ],
  "tool_choice": "auto"
}

When the model calls a tool, the response includes:

json
{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call_abc123",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{\"location\": \"San Francisco\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ]
}

Submit tool results by adding a tool message:

json
{
  "messages": [
    {"role": "user", "content": "What's the weather in San Francisco?"},
    {
      "role": "assistant",
      "tool_calls": [{"id": "call_abc123", "type": "function", "function": {"name": "get_weather", "arguments": "{\"location\": \"San Francisco\"}"}}]
    },
    {
      "role": "tool",
      "tool_call_id": "call_abc123",
      "content": "{\"temperature\": 65, \"condition\": \"sunny\"}"
    }
  ]
}