> For the complete documentation index, see [llms.txt](https://docs.scraperapi.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.scraperapi.com/structured-data-endpoints/ai-results/chatgpt.md).

# ChatGPT

The `ChatGPT Search API` endpoint retrieves response data from the results page of a ChatGPT prompt. Results can be returned in raw HTML format or as a structured JSON format.

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

```bash
curl --request GET \
--url "https://api.scraperapi.com/structured/chatgpt/search/v1?api_key=API_KEY&\
query=QUERY&output_format=OUTPUT_FORMAT&sources=BOOLEAN"
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

payload = {
    'api_key': 'API_KEY',
    'query': 'QUERY',
    'output_format': 'OUTPUT_FORMAT',
    'sources': 'Boolean' #True/False
}

r = requests.get('https://api.scraperapi.com/structured/chatgpt/search/v1',params=payload)

print(r.text)
```

{% endtab %}

{% tab title="NodeJS" %}

```javascript
import fetch from 'node-fetch';

fetch(
  'https://api.scraperapi.com/structured/chatgpt/search/v1?api_key=API_KEY&query=QUERY&output_format=OUTPUT_FORMAT&sources=Boolean'
)
  .then(response => response.json()) 
  .then(data => {
    console.log(data);
  })
  .catch(error => {
    console.error(error);
  });
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php
$url = "https://api.scraperapi.com/structured/chatgpt/search/v1?api_key=API_KEY&query=QUERY&output_format=OUTPUT_FORMAT&sources=Boolean";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);

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

print_r($response);
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
require 'net/http'
require 'json'

params = {
  :api_key => "API_KEY",
  :query => "QUERY",
  :output_format => "OUTPUT_FORMAT",
  :sources => "Boolean" #True/False
}

uri = URI('https://api.scraperapi.com/structured/chatgpt/search/v1')
uri.query = URI.encode_www_form(params)

website_content = Net::HTTP.get(uri)
print(website_content)
```

{% endtab %}

{% tab title="Java" %}

```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static void main(String[] args) {
        try {
            String apiKey = "API_KEY";
            String query = "QUERY";
            String output_format = "output_format";
            String sources = "Boolean"; // True/False

            String urlStr = "https://api.scraperapi.com/structured/chatgpt/search/v1?api_key=" 
                            + apiKey + "&query=" + query + "&coutput_format=" + output_format + "&sources=" + sources;

            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");

            int responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = in.readLine()) != null) {
                    response.append(line);
                }
                in.close();
                System.out.println(response.toString());
            } else {
                System.out.println("Error in API Call. Response code: " + responseCode);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
```

{% endtab %}
{% endtabs %}

**Supported Parameters**

<table><thead><tr><th width="274.2591552734375">Parameters</th><th>Details</th></tr></thead><tbody><tr><td><code>API_KEY</code> (required)</td><td>Your API Key.</td></tr><tr><td><code>QUERY</code> (required)</td><td>The prompt to send to ChatGPT.</td></tr><tr><td><code>SOURCES</code></td><td><p>Determines how <code>sources</code> are handled in the response. Can be set to true or false (default).<br></p><ul><li><code>false</code> (default): We make a best-effort attempt to gather sources, but the request will return a successful (200) response even if no sources are found.</li><li><code>true</code>: Sources are required for a successful (200) response. To increase the likelihood of returning sources, this mode uses premium proxies and additional techniques, which accounts for the additional credits. Also note, if you request HTML output the sources will be expanded.  </li></ul></td></tr><tr><td><code>OUTPUT_FORMAT</code></td><td><code>json</code> returns parsed structured data (default). <code>html</code> returns raw HTML.</td></tr><tr><td><code>INCLUDE_HTML</code></td><td>Determines whether raw HTML is included in the response (this can increase the size of the response). Can be set to true or false (default).</td></tr></tbody></table>

**JSON Response**

```json
{
  "query": "string",              // the submitted prompt, echoed back
  "url": "string|null",           // result/thread URL for the answer
  "model": "string|null",         // model that produced the answer (see notes)
  "answer": "string",             // the full answer, in Markdown
  "sources": [                    // citations / web results (may be empty)
    {
      "title": "string",
      "url": "string",
      "domain": "string",         // bare hostname, www. stripped
      "snippet": "string"         // may be empty when the engine gives none
    }
  ],
  "related_queries": ["string"]   // follow-up suggestions (may be empty)
}
```

**Field Reference**

<table data-search="false"><thead><tr><th width="172.8887939453125">Field</th><th width="165.1480712890625">Type</th><th width="177.5555419921875">Always present?</th><th>Notes</th></tr></thead><tbody><tr><td><code>query</code></td><td>string</td><td>yes</td><td>Echoed prompt</td></tr><tr><td><code>url</code></td><td>string | null</td><td>yes</td><td>Result/thread URL</td></tr><tr><td><code>model</code></td><td>string | null</td><td>yes</td><td>Best-effort. <code>null</code> when not surfaced</td></tr><tr><td><code>answer</code></td><td>string (Markdown)</td><td>yes</td><td>The answer body</td></tr><tr><td><code>sources[]</code></td><td>array of object</td><td>yes (may be empty)</td><td><code>{ title, url, domain, snippet }</code></td></tr><tr><td><code>related_queries[]</code></td><td>array of string</td><td>yes (may be empty)</td><td>Follow-up suggestions</td></tr></tbody></table>

{% hint style="success" %}
All fields are always present in the JSON (empty array or `null` rather than omitted).
{% endhint %}

#### **Specifics**

**`sources`**

* Empty when the answer was not web-grounded (common for  \
  plain ChatGPT if `sources=true` parameter is not used).
* **snippet** is often empty for ChatGPT (its citation chips carry no excerpt).
* **domain** is normalized to a bare hostname (**[www](http://www).** stripped) across all engines.

#### Pricing

ChatGPT queries run on ScraperAPI's standard credit system.

| **Request Type**                | **Credits Charged** |
| ------------------------------- | ------------------- |
| `sources=false` or not included | 25                  |
| `sources=true`                  | 75                  |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.scraperapi.com/structured-data-endpoints/ai-results/chatgpt.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
