Chat API

The Chat API is the core endpoint that powers Miko402's autonomous payment agent. It processes user messages, handles x402 payment flows, and streams responses in real-time.

Endpoint

POST /api/chat

Overview

This endpoint receives user messages, processes them through Google's Gemini AI model, detects x402 payment requirements, executes autonomous blockchain payments within spending limits, and streams responses back to the client.

Configuration

Maximum Duration

export const maxDuration = 50;

The endpoint can run for up to 50 seconds to accommodate complex payment processing, blockchain confirmations, and multi-step x402 workflows.

Vercel platform limits:

  • Hobby: 10 seconds

  • Pro: 60 seconds

  • Enterprise: 900 seconds

Request Format

Headers

Body

With payment context:

Message Structure

Field
Type
Required
Description

messages

Array

Yes

Array of message objects

messages[].role

String

Yes

"user", "assistant", or "system"

messages[].content

String

Yes

Message text content

walletAddress

String

No

User's wallet address for payments

spendingLimits

Object

No

Current spending limit configuration

Response Format

The endpoint returns a streaming response using Server-Sent Events (SSE).

Stream Format

Response Events

Event
Description

0:

Text content chunks

d:

Metadata (finish reason, transaction hash)

e:

Error messages

p:

Payment status updates

Finish Reasons

  • stop — Normal completion

  • payment-complete — Payment processed successfully

  • payment-failed — Payment failed

  • limit-exceeded — Spending limit exceeded

  • insufficient-balance — Not enough funds

  • service-unavailable — x402 service offline

AI Model Configuration

Model

Parameters

Temperature guidance:

  • 0.0–0.3: Deterministic, factual (payment confirmations)

  • 0.4–0.7: Balanced (default)

  • 0.8–1.0: Creative, varied (service discovery)

x402 Integration

Payment Detection

Service Discovery

Payment Processing

System Prompt

The system prompt defines Miko's identity and behavior:

Error Handling

400 Bad Request

402 Payment Required

403 Forbidden

429 Too Many Requests

500 Internal Server Error

503 Service Unavailable

Usage Examples

Using Fetch API

Manual Testing

Performance

Typical Response Times

Operation
Duration

Service discovery

1–2 seconds

Payment processing

2–4 seconds (blockchain confirmation)

Data delivery

1–3 seconds

Total end-to-end

3–8 seconds

Streaming Benefits

  • Users see payment progress in real-time

  • Immediate feedback on transaction status

  • Reduced server memory usage

  • Better error handling with instant feedback

Security Features

Spending Limit Enforcement

Rate Limiting

  • Client-side: 50-second cooldown

  • Server-side: maxDuration prevents long-running abuse

  • Wallet-side: Transaction signing requires user approval

Input Validation

The endpoint validates request structure, wallet address format, spending limit values, and message content safety.

Automated Testing


Need help? Check Troubleshooting or open an issuearrow-up-right.

Last updated