Skip to main content

Querying Agents

Complete guide to accessing AI agents using X402 access tokens with the Nevermined CLI.

Overview

After purchasing a payment plan, you can query AI agents using X402 access tokens. The X402 protocol enables pay-per-use access with automatic credit deduction.

Getting an Access Token

Generate X402 Token

Get an access token for a purchased plan:
Example:
Optional flags:
  • --agent-id — Target agent ID
  • --redemption-limit — Maximum credits that can be redeemed
  • --order-limit — Maximum number of orders
  • --expiration — Token expiration time
Example with options:
Output:

Token Format

The token is a JWT containing:
  • Plan ID
  • Subscriber address
  • Credits information
  • Signature for verification

Save Token for Reuse

Store the token for multiple requests:

Using X402 Tokens

HTTP Requests with curl

Query an agent using curl:

With JavaScript/TypeScript

With Python

Verifying Requests

Manual Verification

Verify that a request is valid before processing (for agent owners):
verify.json:

Automatic Verification (SDK)

For agent implementations, use the SDK for automatic verification:

Settling Credits

Burn Credits After Use

After processing a request, burn the credits (agent owners):
settle.json:

Automatic Settlement (SDK)

Integrate settlement into your agent:

Complete Query Workflow

End-to-End Example

Complete flow from purchase to query:

Advanced Usage

Batch Queries

Process multiple queries efficiently:

Rate-Limited Queries

Respect rate limits:

Error Handling

Handle common errors gracefully:

Monitoring Usage

Track Credit Consumption

Monitor how credits are used per query:

Usage Reports

Generate usage reports:

Best Practices

1. Token Refresh

Tokens may expire, always get fresh tokens for new sessions:

2. Credit Management

Monitor balance before queries:

3. Error Recovery

Implement retries with exponential backoff:

4. Secure Token Storage

Never commit tokens to version control:

5. Rate Limiting

Respect API rate limits to avoid 429 errors:

Common Issues

”Invalid token”

Token may be expired or malformed:

“Insufficient credits”

Your balance is too low:

“402 Payment Required”

The agent requires payment but token is invalid or missing:

Next Steps