Tutorials

Learn & build

Step-by-step guides to monetize APIs and integrate autonomous AI agent payments.

Featured Tutorial

AI Agents15 min read

Autonomous AI Agent Payments

Enable your AI agents to pay for APIs automatically using the x402 protocol. No human intervention required.

The Agentic Economy: As AI agents become more autonomous, they need to pay for services without human approval for each transaction. Apiosk enables machine-to-machine commerce with automatic micropayments.

Step 1: Set Up Agent Wallet

Create a dedicated wallet for your agent. Never use your personal wallet.

import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'

// Generate a new wallet for your agent
const privateKey = generatePrivateKey()
const account = privateKeyToAccount(privateKey)

console.log('Agent wallet address:', account.address)
console.log('Private key (save securely!):', privateKey)

// Fund this wallet with USDC on Base (testnet or mainnet)

Step 2: Configure Environment

Set up your environment variables:

# .env
AGENT_PRIVATE_KEY=0x...your-agent-private-key...
GATEWAY_URL=https://gateway.apiosk.com
NETWORK=base-sepolia  # Use 'base' for production

Step 3: Initialize Agent Client

Use the Apiosk agent client to handle payments automatically:

import { createAgent } from './agent-client'

const agent = createAgent({
  gatewayUrl: process.env.GATEWAY_URL!,
  privateKey: process.env.AGENT_PRIVATE_KEY!,
  network: 'base-sepolia'
})

console.log('Agent initialized:', agent.getAddress())

Step 4: Make Autonomous API Calls

The agent handles 402 responses and payment signing automatically:

// Example: Call an AI inference API
const response = await agent.callApi('/openai/gpt4/completion', {
  method: 'POST',
  body: {
    prompt: 'Explain quantum computing in simple terms',
    max_tokens: 100
  }
})

console.log('AI Response:', response.data)
console.log('Amount paid:', response.paymentInfo.amount, 'USDC')

// Example: Call multiple APIs in sequence
const searchResult = await agent.callApi('/search/web/query', {
  method: 'POST',
  body: { query: 'latest AI research papers' }
})

const summary = await agent.callApi('/ai/summarize', {
  method: 'POST',
  body: { text: searchResult.data.results }
})

Step 5: Monitor Agent Spending

Track your agent's transactions:

// Track spending in your agent
let totalSpent = 0

async function trackedApiCall(endpoint, options) {
  const result = await agent.callApi(endpoint, options)
  totalSpent += parseFloat(result.paymentInfo.amount)

  console.log(`Total spent: $${totalSpent.toFixed(4)} USDC`)

  // Optional: Set spending limit
  if (totalSpent > 10.00) {
    throw new Error('Agent spending limit exceeded')
  }

  return result
}

Security Best Practices

Wallet Isolation

Use dedicated wallets per agent. Never share wallets between agents or with personal funds.

Spending Limits

Fund agents with only what they need. Implement soft limits in code.

Key Management

Store keys in environment variables or secrets manager. Rotate periodically.

Testnet First

Always develop and test on Base Sepolia before using real USDC on mainnet.

Provider5 min read

Sell Your First API

Learn how to register an API endpoint on Apiosk and start earning from each request.

Step 1: Create Your Account

Sign up at market.apiosk.com using your email or wallet. This is where you'll manage your APIs and earnings.

Step 2: Connect Your Wallet

Add your Ethereum wallet address in account settings. This is where you'll receive your earnings in USDC.

Step 3: Register Your API

Click "Add API" and fill in your endpoint details:

  • Name: A descriptive name for your API
  • Upstream URL: Your actual API endpoint (e.g., https://your-server.com/api/data)
  • Price: Amount in USDC per request (e.g., 0.01 for 1 cent)

Step 4: Share Your Apiosk URL

Once registered, you'll get a gateway URL like:

https://gateway.apiosk.com/your-name/your-api/endpoint

Share this URL with your users. They'll pay per request, and you'll earn from each payment.

Step 5: Monitor & Withdraw

Track your earnings in real-time from your dashboard. Withdraw to your wallet whenever you want - there's no minimum.

Buyer7 min read

Consume a Paid API

Learn how to make requests to paid API endpoints using the x402 payment protocol.

Prerequisites

  • A wallet with USDC on Base network
  • An x402-compatible HTTP client

Step 1: Get the API URL

Get the Apiosk gateway URL from the API provider. It will look like:

https://gateway.apiosk.com/provider/api/endpoint

Step 2: Make Initial Request

Make a request without payment to get pricing information:

curl https://gateway.apiosk.com/provider/api/endpoint

# Response: 402 Payment Required
# Headers:
#   X-Payment-Amount: 0.01
#   X-Payment-Asset: USDC
#   X-Payment-Network: base

Step 3: Create Payment Token

Use an x402 library to create a payment token. The token authorizes a specific payment amount.

// Using x402 client library
const payment = await x402.createPayment({
  amount: "0.01",
  asset: "USDC",
  network: "base",
  recipient: "gateway.apiosk.com"
});

Step 4: Make Paid Request

Include the payment token in the X-Payment header:

curl https://gateway.apiosk.com/provider/api/endpoint \
  -H "X-Payment: <your-payment-token>"

# Response: 200 OK
# + API response data

Step 5: Verify Payment

Check response headers to confirm payment was processed:

X-Payment-Status: paid
X-Provider-Earnings: 0.009
X-Commission: 0.001
Get started

Ready to monetize your APIs?

Join the agentic economy. Start accepting AI payments today.