Integrations

Django

Call the Apertis API from a Django view with the OpenAI Python SDK.

Updated
Reading time
1 min

On this page

Add a /chat/ view to a Django project that returns a chat completion from Apertis, using the OpenAI Python SDK.

Install

pip install openai django
django-admin startproject mysite
cd mysite

Set your API key

Create a key in Settings → API Keys, then export it:

export APERTIS_API_KEY="sk-your-api-key"

Minimal view

Replace the project's urls.py with:

mysite/urls.py:

import os

from django.contrib import admin
from django.http import JsonResponse
from django.urls import path
from openai import OpenAI

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


def chat(request):
    q = request.GET.get("q", "Hello! What can you do?")
    completion = client.chat.completions.create(
        model="gpt-5.5",
        messages=[{"role": "user", "content": q}],
    )
    return JsonResponse({"text": completion.choices[0].message.content})


urlpatterns = [
    path("admin/", admin.site.urls),
    path("chat/", chat),
]

Run and test

python manage.py runserver
curl "http://127.0.0.1:8000/chat/?q=Say+hi"

The SDK sends Authorization: Bearer with your key to https://api.apertis.ai/v1. Pick any model ID from the Models page.

Next steps