OpenRouter API Configuration
OpenRouter API Configuration
Udility Diffuser leverages OpenRouter as its inference gateway to access high-performance models like Meta Llama 3. This connection is required to process text prompts into the SVG scripts used for image generation.
1. Obtain an API Key
To use the library, you must have a valid OpenRouter API key.
- Visit OpenRouter.ai.
- Create an account or sign in.
- Navigate to the Keys section and generate a new API key.
2. Setting the Environment Variable
The library automatically looks for an environment variable named OPENROUTER_API_KEY. You can configure this in two ways:
Option A: Runtime Configuration (Python)
If you are working in a Jupyter Notebook, Google Colab, or a script where you want to set the key dynamically, use the os module:
import os
# Set your API key before importing or calling Udility functions
os.environ['OPENROUTER_API_KEY'] = 'your_openrouter_api_key_here'
Option B: Persistent Configuration (Shell)
To avoid hardcoding credentials in your scripts, set the variable in your terminal or environment profile (e.g., .bashrc, .zshrc, or Windows Environment Variables).
Bash/Zsh:
export OPENROUTER_API_KEY='your_openrouter_api_key_here'
Windows (Command Prompt):
setx OPENROUTER_API_KEY "your_openrouter_api_key_here"
3. Connection Details
Udility Diffuser uses the standard OpenAI-compatible client to communicate with OpenRouter. The internal configuration uses the following parameters:
| Parameter | Value |
| :--- | :--- |
| Base URL | https://openrouter.ai/api/v1 |
| Default Model | nousresearch/hermes-3-llama-3.1-405b |
| Auth Header | Authorization: Bearer $OPENROUTER_API_KEY |
Troubleshooting
- EnvironmentError: If the key is missing, the library will raise an
EnvironmentError: The OpenRouter API key is not set.Ensure the variable is set before callingdiffuser.generate_image_from_text(). - Quota Issues: Ensure your OpenRouter account has sufficient credits or access to the free tier models.
- Optional Headers: The library includes default headers for
X-Title(SVG Generator App) to identify requests within the OpenRouter dashboard.