# Quick Start

## Get your API keys

Your API requests are authenticated using API keys in the request headers. Any request that doesn't include an API key will return an error.

You can generate an API key from your [RapidAPI Developer Dashboard](https://rapidapi.com/developer/dashboard) at any time.

After that, you need to subscribe to the [OffloadGPT API](https://rapidapi.com/microdeploy/api/offloadgpt) in order to make requests. Just for testing purposes, there is a **free subscription plan** allowing 1000 requests per month.

In addition, you will need an [OpenAI API key](https://platform.openai.com/account/api-keys) for the internal request to the Chat Completion API.

{% hint style="info" %}
Your OpenAI API Key is used only for the OpenAI API call and **will never be saved, shared or published**. The OpenAI API Key is deleted in memory after the OpenAI API request is performed, avoiding to show it in logs or output debug.
{% endhint %}

{% hint style="info" %}
OpenAI allows you to establish limits on the use of its APIs, so it is highly recommended to enable [API usage limits](https://platform.openai.com/account/billing/limits) to prevent cases of loss or theft of keys.
{% endhint %}

## Make your first request

To make your first request, send an authenticated request to the `stream-chatgpt` endpoint. This will create a new generated endpoint that will store the ChatGPT API response.

Take a look at how you might call this method using any programming language or via `cURL`:

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

```
curl --request POST \
	--url https://offloadgpt.p.rapidapi.com/v1/stream-chatgpt \
	--header 'content-type: application/json' \
	--header 'X-RapidAPI-Host: offloadgpt.p.rapidapi.com' \
	--header 'X-RapidAPI-Key: <REQUIRED>' \
	--header 'X-OpenAI-API-Key: <REQUIRED>' \
	--data '{
    "messages": [
        {
            "role": "system",
            "content": "You are an assistant of an online store selling hardware and I do not want you to talk about anything other than my products"
        },
        {
            "role": "user",
            "content": "Can you resume the pros and cons of the Soundcore by Anker Space Q45 Adaptive Active Noise Cancelling Headphones?"
        }
    ]
}'
```

{% endtab %}

{% tab title="Node" %}

```javascript
const http = require('https');

const options = {
	method: 'POST',
	hostname: 'offloadgpt.p.rapidapi.com',
	port: null,
	path: '/v1/stream-chatgpt',
	headers: {
		'content-type': 'application/json',
		'X-RapidAPI-Host': 'offloadgpt.p.rapidapi.com',
		'X-OpenAI-API-Key': '<REQUIRED>',
		'X-RapidAPI-Key': '<REQUIRED>'
	}
};

const req = http.request(options, function (res) {
	const chunks = [];

	res.on('data', function (chunk) {
		chunks.push(chunk);
	});

	res.on('end', function () {
		const body = Buffer.concat(chunks);
		console.log(body.toString());
	});
});

req.write(JSON.stringify({
  messages: [
    {
      role: 'system',
      content: 'You are an assistant of an online store selling hardware and I do not want you to talk about anything other than my products'
    },
    {
      role: 'user',
      content: 'Can you resume the pros and cons of the Soundcore by Anker Space Q45 Adaptive Active Noise Cancelling Headphones?'
    }
  ]
}));
req.end();
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

url = "https://offloadgpt.p.rapidapi.com/v1/stream-chatgpt"

payload = { "messages": [
		{
			"role": "system",
			"content": "You are an assistant of an online store selling hardware and I do not want you to talk about anything other than my products"
		},
		{
			"role": "user",
			"content": "Can you resume the pros and cons of the Soundcore by Anker Space Q45 Adaptive Active Noise Cancelling Headphones?"
		}
	] }
headers = {
	"content-type": "application/json",
	"X-OpenAI-API-Key": "<REQUIRED>",
	"X-RapidAPI-Key": "<REQUIRED>",
	"X-RapidAPI-Host": "offloadgpt.p.rapidapi.com"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php

$curl = curl_init();

curl_setopt_array($curl, [
	CURLOPT_URL => "https://offloadgpt.p.rapidapi.com/v1/stream-chatgpt",
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_ENCODING => "",
	CURLOPT_MAXREDIRS => 10,
	CURLOPT_TIMEOUT => 30,
	CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	CURLOPT_CUSTOMREQUEST => "POST",
	CURLOPT_POSTFIELDS => json_encode([
		'messages' => [
				[
					'role' => 'system',
					'content' => 'You are an assistant of an online store selling hardware and I do not want you to talk about anything other than my products'
				],
				[
					'role' => 'user',
					'content' => 'Can you resume the pros and cons of the Soundcore by Anker Space Q45 Adaptive Active Noise Cancelling Headphones?'
				]
		]
	]),
	CURLOPT_HTTPHEADER => [
		"X-OpenAI-API-Key: <REQUIRED>",
		"X-RapidAPI-Host: offloadgpt.p.rapidapi.com",
		"X-RapidAPI-Key: <REQUIRED>",
		"content-type: application/json"
	],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
	echo "cURL Error #:" . $err;
} else {
	echo $response;
}
```

{% endtab %}

{% tab title="Javascript" %}

```javascript
const data = JSON.stringify({
	messages: [
		{
			role: 'system',
			content: 'You are an assistant of an online store selling hardware and I do not want you to talk about anything other than my products'
		},
		{
			role: 'user',
			content: 'Can you resume the pros and cons of the Soundcore by Anker Space Q45 Adaptive Active Noise Cancelling Headphones?'
		}
	]
});

const xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener('readystatechange', function () {
	if (this.readyState === this.DONE) {
		console.log(this.responseText);
	}
});

xhr.open('POST', 'https://offloadgpt.p.rapidapi.com/v1/stream-chatgpt');
xhr.setRequestHeader('content-type', 'application/json');
xhr.setRequestHeader('X-OpenAI-API-Key', '<REQUIRED>');
xhr.setRequestHeader('X-RapidAPI-Key', '<REQUIRED>');
xhr.setRequestHeader('X-RapidAPI-Host', 'offloadgpt.p.rapidapi.com');

xhr.send(data);
```

{% endtab %}
{% endtabs %}

## Check the results

If all goes well the expected HTTP response code is `200` , serving the content in JSON format as stated from the `application/json` header Content-Type.&#x20;

This is an example of a valid response:

```json
{
    "status": "success",
    "created_at": 1685617626,
    "conversation_id": "24b94bef-d2a6-4faa-bb20-1429e846c9d3",
    "README": "The `stream_events_url` endpoint below streams data sent by the ChatGPT API. Open it to receive incoming messages.",
    "authorization": {
        "access": "public"
    },
    "endpoints": {
        "status_url": "https://offloadgpt.microdeploy.com/1/r/pub/2023/06/01/11/07/06/24b94bef-d2a6-4faa-bb20-1429e846c9d3.json",
        "stream_events_url": "https://offloadgpt.microdeploy.com/1/r/pub/2023/06/01/11/07/06/24b94bef-d2a6-4faa-bb20-1429e846c9d3.txt",
        "stop_url": "https://offloadgpt.microdeploy.com/1/r/pub/2023/06/01/11/07/06/24b94bef-d2a6-4faa-bb20-1429e846c9d3/stop"
    }
}
```

Next, the first property to check is `status`, where the string `success` informs us that everything went well and the request has been sent to the OpenAI API.

We continue at the `endpoints` property and its subproperty `stream_events_url`. This value is intended to provide an URL where the response is sent in [text/event-stream](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format) format, allowing to create an text stream to the browser using [Javascript server-Sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events).

You can see an example of use of this stream API in this [demo project](https://github.com/pauiglesias/offload-chatgpt-streaming-demo).

Another important endpoint property is `status_url`, an URL intented to show the request status, informing if the request is **still waiting** the OpenAI ChatGPT response, an **error** has occurred or the **request is completed** and the response is available. In addition to the streaming endpoint, this `status_url` method also provides the response as it is generated.


---

# 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://offloadgpt-docs.microdeploy.com/quick-start.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.
