Machine Payments (x402)

Last updated Sep 30, 2026 Machine Payments allows clients to pay for inference calls to select open models from their stablecoin wallet, without having to maintain a prepaid credit balance. To use Machine Payments, you must be based in the United States and have a credit card on file.

The Payment-Method: x402 header tells Cloudflare to negotiate payment through the x402 protocol ↗︎. This header is specific to Cloudflare.

Prerequisites

  • Ensure your Cloudflare account has a credit card on file.
  • Ensure you have authenticated your AI Gateway.

Eligible endpoints

Eligible models

  • z-ai/glm-4.7-flash
  • google/gemma-4-26b-a4b-it
  • openai/gpt-oss-20b
  • alibaba/qwen3.8-27b

Use Machine Payments

Machine Payments works with the Cloudflare API and requires a Cloudflare API token.

Transaction flow

An x402-compatible client handles the challenge and retry automatically.

  1. The client sends an inference request with the Cloudflare-specific Payment-Method: x402 header. This requests x402 payment instead of using the account's configured billing method.
  2. AI Gateway validates the request and returns 402 Payment Required. The standard PAYMENT-REQUIRED response header contains the accepted network, asset, payment scheme, and maximum authorized amount. Most AI Gateway models use the upto payment scheme, which indicates the maximum a client may be charged.

http HTTP/1.1 402 Payment Required PAYMENT-REQUIRED: <base64-encoded payment requirements>

  1. The x402 client decodes the payment requirements. The wallet signs an authorization for up to the maximum amount.
  2. The client retries the same inference request with the signed authorization in the standard PAYMENT-SIGNATURE request header.
  3. Cloudflare verifies the authorization and runs the inference request. Cloudflare settles the actual request cost, which cannot exceed the authorized maximum.
  4. AI Gateway returns the inference response.

REST API

At this time, only the /ai/run endpoint is available with Machine Payments.

Use the Cloudflare API to call third-party models. Pass your Cloudflare API token in the Authorization header, and include Payment-Method: x402 as a header. If the header is not passed, AI Gateway follows the normal credential precedence and may deduct the inference cost from your Unified Billing credit balance.

# Run `wrangler whoami` to get your account ID to replace $CLOUDFLARE_ACCOUNT_ID,
# and `wrangler auth token` to get an auth token to replace $CLOUDFLARE_API_TOKEN.
curl -iX POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Payment-Method: x402" \
  --header "Content-Type: application/json" \
  --data '{
    "model": "z-ai/glm-4.7-flash",
    "input": {
      "max_tokens": 9001,
      "messages": [
        {
          "role": "user",
          "content": "What is Cloudflare?"
        }
      ]
    }
  }'

Refer to REST API for more details.

Was this helpful?

YesNo

On this page

Machine Payments (x402) - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/ai-gateway/features/machine-payments/#page","headline":"Machine Payments (x402)","description":"Use x402 to pay for and authenticate your inference requests.","url":"https://developers.cloudflare.com/ai-gateway/features/machine-payments/","inLanguage":"en","image":"https://developers.cloudflare.com/ai-gateway/features/machine-payments/og.png?v=c0a7655b53b651fc","dateModified":"2026-09-30","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Control traffic to your AI Gateway with fixed or sliding rate limits to prevent excessive costs and suspicious activity. title: Rate limiting image: https://developers.cloudflare.com/ai-gateway/features/rate-limiting/og.png?v=5f4fcbdbf15a0170


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/ai-gateway/llms.txt
Use this file to discover all available pages before exploring further.