Skip to content

Python API Access

KeyMaker is OpenAI-compatible, so you can call campusGenAI’s models from Python with the official openai SDK — just point it at the KeyMaker base URL and authenticate with your API key. This is the foundation for research pipelines, batch processing, and custom applications.

  • Python 3.8 or newer (python --version)
  • A valid KeyMaker API key from your sponsor — see Getting Started
Terminal window
pip install openai

KeyMaker is compatible with the OpenAI SDK because the underlying proxy (LiteLLM) speaks the OpenAI API format. No campusGenAI-specific package is required.

Store your KeyMaker key as an environment variable rather than hardcoding it into your scripts or notebooks — that keeps it out of files you might share or commit.

macOS / Linux:

Terminal window
echo 'export KEYMAKER_API_KEY="YOUR_KEY_HERE"' >> ~/.zshrc && source ~/.zshrc

Windows (PowerShell):

Terminal window
[Environment]::SetEnvironmentVariable("KEYMAKER_API_KEY", "YOUR_KEY_HERE", "User")

Restart your terminal afterward so the variable is available to Python.

Point the OpenAI client at the KeyMaker base URL and read your key from the environment:

import os
import openai
client = openai.OpenAI(
api_key=os.environ["KEYMAKER_API_KEY"],
base_url="https://thekeymaker.umass.edu/v1",
)

List the models your KeyMaker account can use:

models = client.models.list()
print("Available models:")
for model in models.data:
print(f"- {model.id}")

Make note of the model name you want to use (for example, claude-opus-4-8). See the Model Comparison page for what each model is best at.

response = client.chat.completions.create(
model="claude-opus-4-8",
messages=[
{"role": "user", "content": "This is a test request, write a short poem."},
],
max_tokens=50,
)
print(response.choices[0].message.content)

If you see the model’s reply printed, your setup is working — all requests now route through KeyMaker, using the models, permissions, and usage controls of the KeyMaker platform.

Every response carries a usage object showing how many tokens the request consumed — useful for tracking cost against your funding source:

print(response.usage)
# CompletionUsage(completion_tokens=50, prompt_tokens=17, total_tokens=67, ...)

For real use, create the client once and wrap requests in a function you can call throughout your code. Reading the key from the environment means the same function works in a script, a notebook, or a deployed service without changes.

import os
import openai
client = openai.OpenAI(
api_key=os.environ["KEYMAKER_API_KEY"],
base_url="https://thekeymaker.umass.edu/v1",
)
def ask(prompt, model="claude-opus-4-8"):
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content
# Reuse it across your application
for topic in ["photosynthesis", "tariffs", "tectonic plates"]:
print(ask(f"Explain {topic} in one sentence."))

This is the same call you made above, factored so the client is configured in one place and reused — the pattern behind batch processing and research pipelines.

Authentication errors Confirm your key is available to Python:

import os
print(os.environ.get("KEYMAKER_API_KEY"))

If it prints None, the variable isn’t set in this environment — re-run the Save your API key step and restart your terminal (or your notebook kernel). If the key is present but still rejected, verify it’s correct, hasn’t expired or been revoked, and re-copy it from the KeyMaker portal if needed.

Model not found List the models available to your account (the snippet above) and update the model value to match exactly.

Connection or base-URL errors Confirm base_url is exactly https://thekeymaker.umass.edu/v1, including the /v1 suffix.


Your interface may look slightly different depending on your institution’s deployment.