# OpenAI Terraform Provider

The [OpenAI Terraform provider](https://registry.terraform.io/providers/stainless-sdks/openai/latest/docs) provides convenient access to
the [OpenAI REST API](https://platform.openai.com/docs) from Terraform.



## Requirements

This provider requires Terraform CLI 1.0 or later. You can [install it for your system](https://developer.hashicorp.com/terraform/install)
on Hashicorp's website.

## Usage

Add the following to your `main.tf` file:



```hcl
# 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](./examples) folder within this repository, and you can
refer to the full documentation on [the Terraform Registry](https://registry.terraform.io/providers/stainless-sdks/openai/latest/docs).

### 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.

| Property       | Environment variable    | Required | Default value |
| -------------- | ----------------------- | -------- | ------------- |
| webhook_secret | `OPENAI_WEBHOOK_SECRET` | false    | —             |
| project        | `OPENAI_PROJECT_ID`     | false    | —             |
| organization   | `OPENAI_ORG_ID`         | false    | —             |
| api_key        | `OPENAI_API_KEY`        | false    | —             |
| admin_api_key  | `OPENAI_ADMIN_KEY`      | false    | —             |


## Semantic versioning

This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) 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](https://www.github.com/stainless-sdks/openai-terraform/issues) with questions, bugs, or suggestions.

## Contributing

See [the contributing documentation](./CONTRIBUTING.md).
