Skip to content

Latest commit

 

History

History
418 lines (318 loc) · 9.71 KB

File metadata and controls

418 lines (318 loc) · 9.71 KB
description A description of the software development kits (SDKs) that can be used to interact with the AIML API.
icon hammer-brush

Supported SDKs

This page describes the SDK1s that can be used to call our API.

Key Definitions & Notes

The REST API itself is not an SDK. It is the server-side interface that exposes your models over HTTP. It defines endpoints, HTTP methods (POST/GET), required headers, and the structure of request and response JSON. Essentially, it’s the “contract” the server provides for clients to interact with models programmatically.


An SDK (Software Development Kit) is a client-side library that wraps around the REST API. It handles details like building HTTP requests, serializing/deserializing JSON, error handling, retries, and sometimes additional conveniences.

You can skip the SDK and call the REST API directly via cURL, fetch, requests, etc.
The SDK just makes your life easier; the REST API is the “core interface” the SDK talks to.


The following flow shows how a request travels from your code to the model and back. Using an SDK is optional — it simply wraps the REST API for convenience.

Your code → SDK (optional) → REST API → Model → REST API → SDK → Your code

{% hint style="info" %} Comparing requests made with raw REST API and different SDKs, pay attention to the following common aspects:

  • how the Authorization header and the AIML API key are provided,
  • how the POST method and the endpoint URL are specified,
  • how the input parameters are passed. {% endhint %}

{% hint style="success" %} Also take a look at the INTEGRATIONS section — it covers many third-party services and libraries (workflow platforms, coding assistants, etc.) that allow you to integrate our models in various ways. {% endhint %}


REST API

We use the REST API because it’s fast, simple, and easy to understand. Only in Python do you need to import a separate library (requests), while cURL and JavaScript (Node.js) already have built-in support for HTTP requests. Therefore, REST API is used in the documentation examples for all of our models.

Installation

In Python examples, you need to import the requests library. The Node.js and cURL examples do not require any additional imports.

Install the library first:

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

pip install requests

{% endtab %} {% endtabs %}

Import the library in every Python code snippet where you make calls to the REST API.

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

import requests

{% endtab %} {% endtabs %}

Authorization

Our API authorization is based on a Bearer token. Include it in the Authorization HTTP header within the request. Example:

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

  --header 'Authorization: Bearer <YOUR_AIMLAPI_KEY>' \

{% endtab %}

{% tab title="JavaScript" %}

  headers: {
    Authorization: "Bearer <YOUR_AIMLAPI_KEY>",
  },

{% endtab %}

{% tab title="Python" %}

    headers={
        "Authorization": "Bearer <YOUR_AIMLAPI_KEY>",
    },

{% endtab %} {% endtabs %}

Request Example

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

curl --request POST \
  --url https://api.aimlapi.com/chat/completions \
  --header 'Authorization: Bearer <YOUR_AIMLAPI_KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "google/gemma-3-4b-it",
    "messages": [
        {
            "role": "user",
            "content": "What kind of model are you?"
        }
    ],
    "max_tokens": 512
}'

{% endtab %}

{% tab title="JavaScript" %}

fetch("https://api.aimlapi.com/chat/completions", {
  method: "POST",
  headers: {
    Authorization: "Bearer <YOUR_AIMLAPI_KEY>",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "google/gemma-3-4b-it",
    messages: [
      {
        role: "user",
        content: "What kind of model are you?",
      },
    ],
    max_tokens: 512,
  }),
})
  .then((res) => res.json())
  .then(console.log);

{% endtab %}

{% tab title="Python" %}

import requests
import json  # for getting a structured output with indentation

response = requests.post(
    url="https://api.aimlapi.com/chat/completions",
    headers={
        "Authorization": "Bearer <YOUR_AIMLAPI_KEY>",
        "Content-Type": "application/json",
    },
    data=json.dumps(
        {
            "model": "google/gemma-3-4b-it",
            "messages": [
                {
                    "role": "user",
                    "content": "What kind of model are you?",
                },
            ],
            "max_tokens": 512
        }
    ),
)

response.raise_for_status()
print(response.json())

{% endtab %} {% endtabs %}


OpenAI

The OpenAI SDK is a convenient library that simplifies working with our API. It automatically handles JSON responses, includes built-in error handling and retry logic, and provides simple, easy-to-use methods for all API features such as chat, embeddings, and completions.

The AI features that the OpenAI SDK supports
  • Streaming
  • Completions
  • Chat Completions
  • Audio
  • Beta Assistants
  • Beta Threads
  • Embeddings
  • Image Generation
  • File Uploads

{% hint style="info" %} Therefore, we don’t currently have the option to call video models or voice / speech models (STT and TTS) through this SDK. {% endhint %}

Installation

Python

1. Make sure you have Python 3.7+ and pip installed.

2. Install the OpenAI SDK via terminal or Jupyter Notebook:

pip install openai

In Jupyter Notebook, you can also use:

%pip install openai

3. Import the SDK:

import openai

JavaScript (Node.js)

1. Make sure you have Node.js 18+ and npm installed.

2. Install the OpenAI SDK in your project:

npm install openai

3. Import the SDK and initialize the client:

import OpenAI from "openai";

Example Code

{% tabs %} {% tab title="Python" %} {% code overflow="wrap" %}

from openai import OpenAI

# Insert your AIML API key in the quotation marks instead of <YOUR_AIMLAPI_KEY>:
api_key = "<YOUR_AIMLAPI_KEY>" 
base_url = "https://api.aimlapi.com/v1"
user_prompt = "Tell me about San Francisco"

api = OpenAI(api_key=api_key, base_url=base_url)


def main():
    completion = api.chat.completions.create(
        model="google/gemma-3-4b-it",
        messages=[
            {
              "role": "user", 
              "content": user_prompt
            },
        ],
        temperature=0.7,
        max_tokens=256,
    )

    response = completion.choices[0].message.content
    print("User:", user_prompt)
    print("AI:", response)


if __name__ == "__main__":
    main()

{% endcode %} {% endtab %}

{% tab title="JavaScript" %} {% code overflow="wrap" %}

#!/usr/bin/env node

const OpenAI = require("openai");
const baseURL = "https://api.aimlapi.com/v1";
const apiKey = "<YOUR_AIMLAPI_KEY>";
const systemPrompt = "You are a travel agent. Be descriptive and helpful.";
const userPrompt = "Tell me about San Francisco";

const api = new OpenAI({
  apiKey,
  baseURL,
});

const main = async () => {
  try {
    const completion = await api.chat.completions.create({
      model: "gpt-4o",
      messages: [
        {
          role: "system",
          content: systemPrompt,
        },
        {
          role: "user",
          content: userPrompt,
        },
      ],
      temperature: 0.7,
      max_tokens: 256,
    });

    const response = completion.choices[0].message.content;

    console.log("User:", userPrompt);
    console.log("AI:", response);
  } catch (error) {
      console.error("Error:", error.message);
  }
};

main();

{% endcode %} {% endtab %} {% endtabs %}


AI/ML API Python library

We have started developing our own SDK to simplify the use of our service.

{% hint style="success" %} If you’d like to contribute to expanding its functionality, feel free to reach out to us on Discord! {% endhint %}

Installation

After obtaining your AIML API key, create an .env file and copy the required contents into it.

touch .env

Copy the code below, paste it into your .env file, and set your API key in AIML_API_KEY="<YOUR_AIMLAPI_KEY>", replacing <YOUR_AIMLAPI_KEY> with your actual key:

AIML_API_KEY = "<YOUR_AIMLAPI_KEY>"
AIML_API_URL = "https://api.aimlapi.com/v1"

Install the package:

# install from PyPI
pip install aimlapi-sdk-python

Request Example

{% tabs %} {% tab title="Python" %} {% code overflow="wrap" %}

from aiml_api import AIML_API

api = AIML_API()

completion = api.chat.completions.create(
    model = "mistralai/Mistral-7B-Instruct-v0.2",
    messages = [
        {"role": "user", "content": "Explain the importance of low-latency LLMs"},
    ],
    temperature = 0.7,
    max_tokens = 256,
)

response = completion.choices[0].message.content
print("AI:", response)

{% endcode %} {% endtab %} {% endtabs %}

To execute the script, use:

python3 <your_script_name>.py

Next Steps

Footnotes

  1. An SDK (Software Development Kit) is a client-side library that wraps around the REST API.