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

# Query Agent

> Queries a registered AI agent via the Nevermined Proxy or directly, using a valid payment plan and signature-based access token.

Queries a registered AI agent via the Nevermined Proxy or directly, using a valid payment plan and signature-based access token.

This method allows subscribers to send input to an agent's endpoint while securely redeeming credits in the background.

## Example Usage

```ts theme={null}
// Step 1: Get access credentials for the agent
const accessCredentials = await payments.query.getAgentHTTPOptions(planId, agentId)

// Step 2: Query the agent with the credentials
const result = await payments.query(agentId, accessCredentials, { prompt: "Write a smart contract." })

// OR use with standard fetch
const options = await payments.query.getAgentHTTPOptions(planId, agentId)
const response = await fetch(agentEndpoint, {
  method: 'POST',
  headers: options.headers,
  body: JSON.stringify({ prompt: "Write a smart contract." })
})
```

## Parameters

* **planId**: The ID of the payment plan the subscriber has purchased
* **agentId**: The ID of the agent being queried
* **accessCredentials**: Auth credentials object containing:
  * `accessToken`: JWT token for authentication
  * `neverminedProxyUri`: Proxy URI for requests
* **payload**: Request payload, typically includes a prompt or other structured input

## Response

The access credentials object structure:

```json theme={null}
{
  "accessToken": "eJyNj0sKgDAURP9lJQ....",
  "neverminedProxyUri": "https://proxy.testing.nevermined.app"
}
```

## Notes

* Nevermined will automatically:
  * Validate the subscriber's plan and credits
  * Send the request to the agent's registered endpoint
  * Redeem the appropriate number of credits (defined by the plan)
* Fails with a 402 if the subscriber is unauthorized


## OpenAPI

````yaml POST /agent/{agentId}/query
openapi: 3.1.0
info:
  title: Nevermined API
  description: >-
    API for managing AI agents, payment plans, and subscriptions in the
    Nevermined ecosystem
  license:
    name: MIT
  version: 1.0.0
servers:
  - url: http://sandbox.mintlify.com
security:
  - bearerAuth: []
paths:
  /agent/{agentId}/query:
    post:
      tags:
        - Agents
      summary: Query Agent
      description: >-
        Queries a registered AI agent via the Nevermined Proxy or directly,
        using a valid payment plan and signature-based access token.
      operationId: queryAgent
      parameters:
        - name: agentId
          in: path
          description: The unique identifier of the agent to query
          required: true
          schema:
            type: string
      requestBody:
        description: Query payload
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                prompt:
                  type: string
      responses:
        '200':
          description: Query successful
          content:
            application/json:
              schema:
                type: object
        '402':
          description: Payment Required
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer

````