This Terraform provider allows you to manage OpenRouter API keys programmatically using the OpenRouter Key Management API.
- Terraform 0.13.x or later
- Go 1.21 or later (for building from source)
- An OpenRouter Provisioning API key
Before using this provider, you need to create a Provisioning API key:
- Go to the Provisioning API Keys page
- Click "Create New Key"
- Complete the key creation process
Important: Provisioning keys cannot be used to make API calls to OpenRouter's completion endpoints - they are exclusively for key management operations.
terraform {
required_providers {
openrouter = {
source = "hashicorp.com/dev/openrouter"
version = "~> 1.0"
}
}
}
provider "openrouter" {
# Optional: Base URL for OpenRouter API (defaults to https://openrouter.ai/api/v1)
# base_url = "https://openrouter.ai/api/v1"
# Required: Provisioning API key for managing OpenRouter API keys
provisioning_api_key = var.openrouter_provisioning_api_key
}You can set the following environment variables instead of hardcoding values:
export OPENROUTER_PROVISIONING_API_KEY="your-provisioning-key"
export OPENROUTER_BASE_URL="https://openrouter.ai/api/v1" # OptionalManages an OpenRouter API key.
# Create an API key with a credit limit
resource "openrouter_api_key" "customer_key" {
name = "Customer Instance Key"
label = "customer-123"
limit = 1000.0
}
# Create an unlimited API key
resource "openrouter_api_key" "unlimited_key" {
name = "Development Key"
label = "dev-environment"
}
# Create a disabled API key
resource "openrouter_api_key" "disabled_key" {
name = "Disabled Key"
disabled = true
limit = 50.0
}name- (Required) The name of the API keylabel- (Optional) Optional label for the API keylimit- (Optional) Credit limit for the API keydisabled- (Optional) Whether the API key is disabled (default: false)include_byok_in_limit- (Optional) Whether to include BYOK usage in the limit
hash- The hash identifier of the API keykey_value- The actual API key value (only available on creation, sensitive)usage- Current usage of the API keycreated_at- Creation timestampupdated_at- Last update timestamp
Automatically create unique API keys for each customer instance:
resource "openrouter_api_key" "customer" {
for_each = var.customers
name = "Customer ${each.key}"
label = "customer-${each.key}"
limit = each.value.credit_limit
}
output "customer_api_keys" {
value = {
for k, v in openrouter_api_key.customer : k => v.key_value
}
sensitive = true
}Implement automatic key rotation for security compliance:
resource "openrouter_api_key" "rotated_key" {
name = "Production Key ${formatdate("YYYY-MM", timestamp())}"
label = "prod-${formatdate("YYYY-MM", timestamp())}"
limit = var.production_limit
lifecycle {
create_before_destroy = true
}
}
# Disable old key when new one is created
resource "openrouter_api_key" "old_key" {
count = var.disable_old_key ? 1 : 0
name = "Production Key ${formatdate("YYYY-MM", timeadd(timestamp(), "-720h"))}"
disabled = true
depends_on = [openrouter_api_key.rotated_key]
}git clone https://github.com/knowit-solutions-cocreate/terraform-provider-openrouter
cd terraform-provider-openrouter
go buildgo test ./...For local development, you can build and install the provider locally:
go build -o terraform-provider-openrouter
mkdir -p ~/.terraform.d/plugins/hashicorp.com/dev/openrouter/1.0.0/darwin_arm64/
cp terraform-provider-openrouter ~/.terraform.d/plugins/hashicorp.com/dev/openrouter/1.0.0/darwin_arm64/- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for your changes
- Run tests and ensure they pass
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.