Skip to content
Primary navigation

OpenAI Terraform Provider

OpenAI Terraform Provider

The OpenAI Terraform provider provides convenient access to the OpenAI REST API from Terraform.

Requirements

This provider requires Terraform CLI 1.0 or later. You can install it for your system on Hashicorp’s website.

Usage

Add the following to your main.tf file:

# Declare the provider and version
terraform {
  required_providers {
    SDK_ProviderTypeName = {
      source  = "stainless-sdks/openai"
      version = "~> 0.0.1"
    }
  }
}

# Initialize the provider
provider "openai" {
  api_key = "My API Key" # or set OPENAI_API_KEY env variable
  admin_api_key = "My Admin API Key" # or set OPENAI_ADMIN_KEY env variable
  organization = "My Organization" # or set OPENAI_ORG_ID env variable
  project = "My Project" # or set OPENAI_PROJECT_ID env variable
  webhook_secret = "My Webhook Secret" # or set OPENAI_WEBHOOK_SECRET env variable
}

# Configure a resource
resource "openai_chat_completion" "example_chat_completion" {
  messages = [{
    content = "Say this is a test"
    role = "user"
    name = "name"
  }]
  model = "gpt-4o"
  audio = {
    format = "wav"
    voice = "string"
  }
  frequency_penalty = -2
  function_call = "none"
  functions = [{
    name = "name"
    description = "description"
    parameters = {
      foo = "bar"
    }
  }]
  logit_bias = {
    foo = 0
  }
  logprobs = true
  max_completion_tokens = 0
  max_tokens = 0
  metadata = {
    foo = "string"
  }
  modalities = ["text"]
  n = 1
  parallel_tool_calls = true
  prediction = {
    content = "string"
    type = "content"
  }
  presence_penalty = -2
  prompt_cache_key = "prompt-cache-key-1234"
  prompt_cache_retention = "in_memory"
  reasoning_effort = "none"
  response_format = {
    type = "text"
  }
  safety_identifier = "safety-identifier-1234"
  seed = -9007199254740991
  service_tier = "auto"
  stop = <<EOT

  EOT
  store = true
  stream = false
  stream_options = {
    include_obfuscation = true
    include_usage = true
  }
  temperature = 1
  tool_choice = "none"
  tools = [{
    function = {
      name = "name"
      description = "description"
      parameters = {
        foo = "bar"
      }
      strict = true
    }
    type = "function"
  }]
  top_logprobs = 0
  top_p = 1
  user = "user-1234"
  verbosity = "low"
  web_search_options = {
    search_context_size = "low"
    user_location = {
      approximate = {
        city = "city"
        country = "country"
        region = "region"
        timezone = "timezone"
      }
      type = "approximate"
    }
  }
}

Initialize your project by running terraform init in the directory.

Additional examples can be found in the ./examples folder within this repository, and you can refer to the full documentation on the Terraform Registry.

Provider Options

When you initialize the provider, the following options are supported. It is recommended to use environment variables for sensitive values like access tokens. If an environment variable is provided, then the option does not need to be set in the terraform source.

PropertyEnvironment variableRequiredDefault value
webhook_secretOPENAI_WEBHOOK_SECRETfalse
projectOPENAI_PROJECT_IDfalse
organizationOPENAI_ORG_IDfalse
api_keyOPENAI_API_KEYfalse
admin_api_keyOPENAI_ADMIN_KEYfalse

Semantic versioning

This package generally follows SemVer conventions, though certain backwards-incompatible changes may be released as minor versions:

  1. Changes to library internals which are technically public but not intended or documented for external use. (Please open a GitHub issue to let us know if you are relying on such internals.)
  2. Changes that we do not expect to impact the vast majority of users in practice.

We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.

We are keen for your feedback; please open an issue with questions, bugs, or suggestions.

Contributing

See the contributing documentation.