OpenRouter API Setup
OpenRouter API Configuration
Udility Diffuser leverages the OpenRouter API to access Meta Llama-3.5 models for generating SVG-based illustrations. You must provide a valid API key for the library to authenticate requests.
1. Obtain an API Key
To get your API key:
- Visit OpenRouter.ai.
- Create an account or sign in.
- Navigate to the Keys section in your dashboard.
- Create a new key and copy it.
Note: OpenRouter provides access to various models. Udility Diffuser is optimized to work with Llama-3.5 variants, including free versions like Nous Capybara.
2. Configure Your Environment
The library expects the API key to be stored in an environment variable named OPENROUTER_API_KEY. You can set this up in two ways:
Option A: Within your Python script (Recommended for Notebooks/Colab)
Set the variable at the start of your script before importing the diffuser module.
import os
# Set your OpenRouter API key
os.environ['OPENROUTER_API_KEY'] = 'your-api-key-here'
from Udility import diffuser
Option B: Via Terminal (Persistent across sessions)
If you are working in a local development environment, you can set the environment variable globally.
For Linux/macOS:
export OPENROUTER_API_KEY='your-api-key-here'
For Windows (Command Prompt):
setx OPENROUTER_API_KEY "your-api-key-here"
3. Verification
Once the key is set, the library will automatically initialize the OpenAI client pointing to the OpenRouter base URL (https://openrouter.ai/api/v1).
If the key is missing, the library will raise an EnvironmentError when you attempt to generate an image:
# Error triggered if key is not found:
# EnvironmentError: The OpenRouter API key is not set. Please set it as an environment variable.
Security Best Practices
- Do not hardcode keys: Avoid committing scripts containing your raw API key to public version control systems like GitHub.
- Use .env files: For local projects, consider using a
.envfile and thepython-dotenvpackage to manage your secrets securely.