Fondazione ITS INCOM

Examples · Programming

Your first API call in Python

Create a key, install the OpenAI library and call a model from your own code.

What you need

Python 3 on your computer and your ITS INCOM AI account.

1. Create a key

At my.ai.itsincom.org, open API keys and create a key. Copy it straight away: it is shown only once.

A key is like a password: do not share it, and never put it in code you publish (GitHub included). Every call uses your credits.

2. Install the library

pip install openai

ITS INCOM AI speaks the same language as OpenAI: you use their library and change only the address and the key.

3. Put the key in an environment variable

macOS and Linux:

export API_KEY="sk-..."

Windows (PowerShell):

$env:API_KEY = "sk-..."

4. The program

import os

from openai import OpenAI

client = OpenAI(
    base_url="https://api.ai.itsincom.org/v1",
    api_key=os.environ["API_KEY"],
)

answer = client.chat.completions.create(
    model="gemma-4-26b",
    messages=[
        {"role": "system", "content": "Answer in English, briefly."},
        {"role": "user", "content": "Write a Python function that checks whether a string is a palindrome."},
    ],
)

print(answer.choices[0].message.content)
print("tokens used:", answer.usage.total_tokens)

5. Exercise

All the details are in the documentation.

All examples