# AI Agent Service APIs

This service is part of the **Crypto.com AI Agent SDK**, designed to facilitate querying blockchain services via Natural Language Processing (NLP) integrated with OpenAI. The core logic of the service interprets user queries, matches them with blockchain functions, and interacts with blockchain nodes and explorers.

### Background

The Crypto.com AI Agent Service API integrates multiple technologies to provide a seamless interaction with blockchain services via natural language queries. The core logic is divided into several components:

1. **Agent Logic**: This stores the core mapping logic between user input and blockchain functions. It uses OpenAI's NLP capabilities to map the user’s query to the corresponding blockchain function.
2. **Blockchain Service**: A collection of blockchain commands such as `getLatestBlock`, `createWallet`, etc. These commands interact with blockchain nodes to retrieve or perform actions.
3. **Explorer Integration**: This component interacts with blockchain explorers to retrieve additional blockchain data using the Cronos explorer APIs.

* API url: [https://ai-agent-api.crypto.com/api/v1/cdc-ai-agent-service](https://ai-agent-api.crypto.com/api/v1/cdc-ai-agent-service/query) (***Rate limited to 50 requests per minute***)

### Features

* **NLP Query Mapping**: Maps user queries to blockchain functions using OpenAI.
* **Blockchain Commands**: Perform blockchain-specific commands such as retrieving blocks, creating wallets, and more.
* **Explorer API Integration**: Fetch blockchain data from the Cronos explorer APIs.
* **Health Monitoring**: A health check route to monitor the API’s status and uptime.

### How It works

The service exposes two main API routes:

1. **Health Check Route**: Provides basic uptime and response time of the service, allowing users or automated systems to monitor the API.
2. **Query Route**: Allows the user to input a query in natural language, which is processed by the core Agent logic. This logic, powered by OpenAI, maps the input to a specific blockchain function and executes the corresponding action on the blockchain.

The architecture:

* The **query server** accepts a query and processes it through the agent logic.
* **OpenAI** processes the query using NLP and matches it with a relevant function.
* The matched function is then executed by interacting with the **blockchain service** or **explorer APIs** to retrieve or perform the desired action.

### API Endpoints

**Health Check**

* **Endpoint**: `/healthcheck`
* **Method**: `GET`
* **Description**: Returns the uptime and health status of the service.

**Example**:&#x20;

```
curl https://ai-agent-api.crypto.com/healthcheck
```

**Response Example**:

```
{
  "status": "success",
  "result": {
    "uptime": 120.34,
    "responsetime": [0, 252939],
    "message": "OK",
    "timestamp": 1632846348163
  }
}
```

**Query Route**

* **Endpoint**: `/api/v1/cdc-ai-agent-service/query`
* **Method**: `POST`
* **Description**: Takes a natural language query, maps it to a blockchain command via OpenAI, and executes the command.

Example: Getting the latest block - With `get latest block`

{% tabs %}
{% tab title="Request Body Example:" %}

```
{
  "query": "get latest block",
  "options": {
    "openAI": {
      "apiKey": "<your-openai-api-key>"
    },
    "chain": {
      "id": 240,
      "name": "cronos-zkevm-testnet",
      "rpc": "https://testnet.zkevm.cronos.org"
    },
    "explorer": {
      "apiKey": "<your-explorer-api-key>"
    },
    "wallet": {
      "mnemonic": "<your-mnemonic-phrase>"
    }
  }
}
```

{% endtab %}

{% tab title="cURL example:" %}

```
curl --location 'https://ai-agent-api.crypto.com/api/v1/cdc-ai-agent-service/query' \
--header 'Content-Type: application/json' \
--data '{
    "query": "What is the latest block?",
    "options": {
        "openAI": {
            "apiKey": "<FILL-IN>"
        },
        "chainId": 388,
        "explorerKeys": {
            "cronosZkEvmKey": "<FILL-IN>"
        }
    }
}'
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
Here are the key details for:

**Cronos zkEVM**

* [Network config](https://docs-zkevm.cronos.org/for-developers/develop-smart-contracts-and-dapps#overview)
* [Explorer](https://docs-zkevm.cronos.org/for-developers/developer-tools/developer-portal-apis)[ API Key](https://docs-zkevm.cronos.org/for-developers/developer-tools/developer-portal-apis)

**Cronos EVM**

* [Network config](https://docs.cronos.org/cronos-chain-protocol/cronos-general-faq#what-is-the-chain-id-for-cronos-mainnet)
* [Explorer](https://docs.cronos.org/block-explorers/block-explorer-and-api-keys)[ API Key](https://docs.cronos.org/block-explorers/block-explorer-and-api-keys)
  {% endhint %}

**Response Example**:

```
{
  "status": "success",
  "result": {
    "action": "getBlock",
    "message": "Retrieved latest block",
    "data": {
      "blockNumber": 123456,
      "timestamp": "2023-09-12T10:12:15Z"
    }
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ai-agent-sdk-docs.crypto.com/outdated-contents/crypto.com-ai-agent-client/ai-agent-service-apis.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
