# Model API guide

## Pre-requirement

### Manual creation via website

* Generate API Key via [#acquiring-the-api-key](https://docs.flock.io/flock-products/ai-marketplace/getting-started-manual-creation#acquiring-the-api-key "mention")
* Create Model via [#model-customisation](https://docs.flock.io/flock-products/ai-marketplace/guideline-manual#model-customisation "mention")

## Technology Stack

* TypeScript
* JavaScript

### API Call example

{% openapi src="<https://742781353-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1RpcbvTSHzzPwOSUvgKU%2Fuploads%2Fd7duzxHXRu3ZnLBllCJP%2Fopenapi.json?alt=media&token=3e2b3b87-7de2-4e11-9ce6-021eac3a97e2>" path="/chat/conversational\_rag\_chat" method="post" %}
[openapi.json](https://742781353-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1RpcbvTSHzzPwOSUvgKU%2Fuploads%2Fd7duzxHXRu3ZnLBllCJP%2Fopenapi.json?alt=media\&token=3e2b3b87-7de2-4e11-9ce6-021eac3a97e2)
{% endopenapi %}

## How it Works

### Setup

#### Package installation

```bash
npm install --save axios dotenv
# or
yarn add axios dotenv
# or 
pnpm add axios dotenv
```

### `.env`

* Create an `.env` file in the root directory or in your existing env file, add the followings:

```
// Some code
FLOCK_BOT_API_KEY= "<your_wallet_address_from_api_generation>"
FLOCK_BOT_ENDPOINT="https://rag-chat-ml-backend-prod.flock.io"
```

### Step by Step guide

* Create a `flockModel.ts`
* Create a `async/await` function `flockModel` wrapping the API call
  * use `axios.post` method for request
  * We will pass two parameters
    * `question`
    * `knowledge_source_id`
  * get the response of the call
  * We chose to use `try/catch` to handle any error during the call to debug the process.

<pre class="language-tsx"><code class="lang-tsx">import axios from "axios";
import * as dotenv from "dotenv";

// Load environment variables from .env file
dotenv.config({ path: "./.env" }); // Make sure the path is correct

async function main(prompt: string) {
  console.log("Prompt:", prompt);

  try {
    // Construct the request payload
    const payload = {
      question: prompt,
      chat_history: [],
      knowledge_source_id: "&#x3C;model_id>", // replace with your model id
    };

    // Set the headers
    const headers = {
      "x-api-key": process.env.FLOCK_BOT_API_KEY, // Ensure API key is set in .env
    };

    // Send POST request using axios
<strong>    const response = await axios.post(
</strong>      `${endpoint}/chat/conversational_rag_chat`,
      payload,
      {
        headers,
      }
    );


    // Output the response data
    console.log(response.data);
  } catch (error) {
    console.error("Error:", error);
  }
}

// Example call to the main function
main("Can you figure out how to make a chatbot?");
</code></pre>
