> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chance.cc/llms.txt
> Use this file to discover all available pages before exploring further.

# Buy credits with x402

> Pays USDC on Base (eip155:8453) over x402 v2. Name the account with an API key or with `?account=`. Without a payment the response is 402 with a PAYMENT-REQUIRED header. Sign the EIP-3009 authorization it describes and retry with PAYMENT-SIGNATURE. Credits are added after settlement, once per authorization. A plain USDC transfer credits nothing.



## OpenAPI

````yaml /openapi.json post /api/v1/credits/topup
openapi: 3.0.3
info:
  title: Chance API
  version: 1.0.0
  description: >-
    Verify an agent's proposed action against its rules, manage escrow wallets,
    and buy credits. Every verification returns a signed, hash-chained receipt.
servers:
  - url: https://harness.chance.cc
    description: Production
security:
  - ApiKeyAuth: []
  - BearerAuth: []
tags:
  - name: Verification
    description: Check an action against its rules and fetch past verdicts.
  - name: Credits
    description: Balance and x402 top-ups.
  - name: Escrow wallets
    description: Wallets that only move funds through a verified proposal.
  - name: Trader
    description: Liveness reports from trader containers.
  - name: Gas sponsorship
    description: Sponsored gas for trader instances on Polygon.
  - name: Status
    description: Public deployment facts for checking receipts.
paths:
  /api/v1/credits/topup:
    post:
      tags:
        - Credits
      summary: Buy credits with x402
      description: >-
        Pays USDC on Base (eip155:8453) over x402 v2. Name the account with an
        API key or with `?account=`. Without a payment the response is 402 with
        a PAYMENT-REQUIRED header. Sign the EIP-3009 authorization it describes
        and retry with PAYMENT-SIGNATURE. Credits are added after settlement,
        once per authorization. A plain USDC transfer credits nothing.
      operationId: topupCredits
      parameters:
        - name: credits
          in: query
          required: true
          description: Credits to buy. Integer, 20 to 10,000 by default. 1 credit = $0.05.
          schema:
            type: integer
            minimum: 20
            maximum: 10000
        - name: account
          in: query
          required: false
          description: >-
            Public top-up code (`chance_topup_…`). Use it instead of an API key
            to pay for another account. Ignored when an API key is sent.
          schema:
            type: string
      responses:
        '200':
          description: Settled and credited. PAYMENT-RESPONSE carries the settlement.
          headers:
            PAYMENT-RESPONSE:
              description: Base64 x402 v2 settlement response.
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TopupReceipt'
              example:
                credited: 200
                usd: '10.00'
                creditsRemaining: 203
                txHash: 0x…
                payer: 0x…
                replayed: false
        '400':
          description: '`credits` is missing, not an integer, or out of range.'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                error: invalid_credits
                message: credits must be between 20 and 10000 …
        '401':
          description: No account named, or the API key is unknown or revoked.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              examples:
                e0:
                  summary: missing_account
                  value:
                    error: missing_account
                    message: >-
                      identify the account to credit: send an x-api-key header,
                      or ?account=<chance_topup_…> code
                e1:
                  summary: invalid or revoked API key
                  value:
                    error: invalid or revoked API key
        '402':
          description: >-
            Payment required, or the attached payment failed. The
            PAYMENT-REQUIRED header always carries fresh terms. After a
            settlement failure, PAYMENT-RESPONSE is also set with `success:
            false`.
          headers:
            PAYMENT-REQUIRED:
              description: >-
                Base64 x402 v2 PaymentRequired: scheme `exact`, network
                eip155:8453, USDC, amount = credits × $0.05.
              schema:
                type: string
            PAYMENT-RESPONSE:
              description: Only after a settlement failure.
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TopupPaymentRequired'
              examples:
                required:
                  summary: No payment attached
                  value:
                    error: payment_required
                    message: 'Payment required: $10.00 USDC on Base for 200 credits. …'
                    x402Version: 2
                    credits: 200
                    usd: '10.00'
                v1:
                  summary: x402 v1 X-PAYMENT header sent
                  value:
                    error: x402 v1 payments are not supported …
                    message: …
                    x402Version: 2
                    credits: 200
                    usd: '10.00'
                delegated:
                  summary: Smart or delegated payer rejected
                  value:
                    error: This wallet (0x…) can't complete the payment …
                    message: …
                    x402Version: 2
                    credits: 200
                    usd: '10.00'
                    code: wallet_unsupported
        '404':
          description: Unknown account code.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                error: invalid_account
                message: unknown top-up account code
        '409':
          description: This authorization was already submitted for a different account.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                error: payment_claimed
                message: >-
                  this payment authorization was already submitted for a
                  different account
        '502':
          description: >-
            The facilitator is unavailable. Retry with the same
            PAYMENT-SIGNATURE; crediting is idempotent.
          headers:
            Retry-After:
              description: Seconds to wait.
              schema:
                type: integer
                example: 5
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                error: facilitator_unavailable
                message: … retry with the same payment
        '503':
          description: Top-ups are not enabled on this deployment.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              example:
                error: topup_not_configured
                message: Credit top-ups are not enabled on this deployment.
      security:
        - ApiKeyAuth: []
        - BearerAuth: []
        - {}
components:
  schemas:
    TopupReceipt:
      type: object
      properties:
        credited:
          type: integer
          example: 200
        usd:
          type: string
          example: '10.00'
        creditsRemaining:
          type: integer
          description: >-
            New balance. Only returned when the request authenticated with an
            API key.
          example: 203
        txHash:
          type: string
          nullable: true
          description: Base settlement transaction.
        payer:
          type: string
          description: Address that signed the payment.
        replayed:
          type: boolean
          description: >-
            True when this payment was already credited and the original result
            is returned.
    Error:
      type: object
      required:
        - error
      properties:
        error:
          type: string
          description: >-
            An error code (for example `out_of_credits`) or a short sentence
            (for example `intent is required`).
        message:
          type: string
          description: Detail, when present.
    TopupPaymentRequired:
      type: object
      properties:
        error:
          type: string
          description: >-
            `payment_required` when no payment was attached; otherwise the
            failure message.
        message:
          type: string
        x402Version:
          type: integer
          enum:
            - 2
        credits:
          type: integer
        usd:
          type: string
        code:
          type: string
          enum:
            - wallet_unsupported
          description: >-
            Set when the payer is a smart or EIP-7702-delegated account whose
            signature was rejected. Retrying from that wallet will not help.
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key
      description: An API key (`chance_sk_live_…`) from https://harness.chance.cc/keys.
    BearerAuth:
      type: http
      scheme: bearer
      description: >-
        The same API key sent as `Authorization: Bearer <key>`. If both headers
        are sent, `x-api-key` is used.

````