> For the complete documentation index, see [llms.txt](https://api.pdf.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://api.pdf.ai/v2/ask.md).

# Ask

Ask questions about your parsed PDF documents and get answers. You can query multiple documents at once. Includes the full document context for precise answers.

<mark style="color:green;">`POST`</mark> `https://pdf.ai/api/v2/ask`

`prompt` one or more of your parsed documents (`docIds`) and get an answer based on their content.

#### Previously Parsed Documents

To use this endpoint the provided document IDs must have been previously parsed. See the [Parse](/v2/parse.md) endpoint here.

#### Sample Requests

Here's how you can make requests to the endpoint using different programming languages.

{% tabs %}
{% tab title="cURL" %}

```shellscript
curl -X POST https://pdf.ai/api/v2/ask \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "What is the main topic of this document?",
    "docIds": ["your-document-id-1", "your-document-id-2"]
  }'
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

url = "https://pdf.ai/api/v2/ask"
headers = {
    "X-API-Key": "YOUR_API_KEY",
    "Content-Type": "application/json"
}

data = {
    "prompt": "What is the main topic of this document?",
    "docIds": ["your-document-id-1", "your-document-id-2"]
}

response = requests.post(url, headers=headers, json=data)
print(response.json())
```

{% endtab %}

{% tab title="Node.js" %}

```javascript
const axios = require('axios');

axios.post('https://pdf.ai/api/v2/ask', 
  {
    prompt: 'What is the main topic of this document?',
    docIds: ['your-document-id-1', 'your-document-id-2']
  },
  {
    headers: {
      'X-API-Key': 'YOUR_API_KEY',
      'Content-Type': 'application/json'
    }
  }
).then(response => {
  console.log(response.data);
});
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php
$url = "https://pdf.ai/api/v2/ask";
$apiKey = "YOUR_API_KEY";

$data = json_encode([
    "prompt" => "What is the main topic of this document?",
    "docIds" => ["your-document-id-1", "your-document-id-2"]
]);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "X-API-Key: $apiKey",
    "Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

echo $response;
?>
```

{% endtab %}
{% endtabs %}

Please replace placeholders like `<YOUR_API_Key>` with actual values.

#### Headers

| Name                                        | Type   | Description |
| ------------------------------------------- | ------ | ----------- |
| X-API-Key<mark style="color:red;">\*</mark> | string | \<API-Key>  |

#### Request Format

Content-Type: application/json

#### Request Parameters

| Parameter | Type      | Required | Description                                                           |
| --------- | --------- | -------- | --------------------------------------------------------------------- |
| prompt    | string    | Yes      | The question or query you want to ask about the documents.            |
| docIds    | string\[] | Yes      | Array of document IDs to query. All documents must be already parsed. |

#### Response format

{% tabs %}
{% tab title="200 Parsed content" %}
{% code overflow="wrap" %}

```json
{
  "success": true,
  "answer": "The main topics discussed in these documents are..."
}
```

{% endcode %}
{% endtab %}

{% tab title="401 Invalid API key" %}

```json
{
    "error": "Invalid API key"
}
```

{% endtab %}

{% tab title="400: Bad Request No API key or docId is present" %}

```json
{
    "error": "No API key present"
}
```

{% endtab %}
{% endtabs %}

#### Credit Usage

Credits are calculated based on the total number of pages across all documents queried

| Component   | Condition      | Credit Calculation           |
| ----------- | -------------- | ---------------------------- |
| Ask credits | Always charged | 3 credits × total page count |

#### Total Credit Formula

`Total credits = Total page count * 3`

#### Example

* Document 1: 10 pages
* Document 2: 5 pages
* Total Credits Used: (10 + 5) × 3 = 45 credits
