# `SystemOneClient.Provider`
[🔗](https://github.com/schainks/system_one_client/blob/v0.5.0/lib/system_one_client/provider.ex#L1)

Resolves where a request goes and how its response is shaped.

| provider | endpoint | auth | default model | envelope |
| --- | --- | --- | --- | --- |
| `:typesafe` (default) | `https://api.typesafe.ai/v1/systemone` | `TYPESAFE_API_KEY` | `jev-latest` | top level |
| `:cloudflare` | `.../accounts/{account_id}/ai/run/@cf/cloudflare/clef` | `CLOUDFLARE_API_TOKEN` | `@cf/cloudflare/clef` | `result` |
| `:openrouter` | `https://openrouter.ai/api/alpha/decisions` | `OPENROUTER_API_KEY` | `typesafe/jev-1.13` | top level |
| `:openai` | `https://api.openai.com/v1/decisions` | `OPENAI_API_KEY` | `gpt-6-luna` | translated (see `SystemOneClient.Wire.OpenAIDecisions`) |
| `:gemini` | `https://generativelanguage.googleapis.com/v1beta/decisions` | `GEMINI_API_KEY`, sent as `x-goog-api-key` | `gemini-decision-1` | top level (request translated, see `SystemOneClient.Wire.GeminiDecisions`) |
| `:flide` | `FLIDE_URL`, else `http://localhost:8000/v1/systemone` | optional `FLIDE_API_KEY` | none (the server's own) | top level |
| `:azure_foundry` | `url:`, else `AZURE_FOUNDRY_ENDPOINT` + `/providers/microsoft/v1/systemone` | `api_key:` or `AZURE_FOUNDRY_API_KEY`, sent as `api-key`; or `bearer_token:` or `AZURE_FOUNDRY_BEARER_TOKEN` | none: `model:` (the deployment name) is required | top level |
| `:compatible` | `url:` (required) | optional | none | top level |

`url:` and `model:` options always win over provider defaults. Keys come from `api_key:`
or the provider's environment variable; `env:` can replace the environment in tests.

`:openai` also takes `organization:` and `project:` (falling back to `OPENAI_ORG_ID` and
`OPENAI_PROJECT_ID`), sent as the `OpenAI-Organization` and `OpenAI-Project` headers.
Unset or empty values send no header.

# `auth`

```elixir
@type auth() :: {:bearer, String.t()} | {:header, String.t(), String.t()}
```

# `envelope`

```elixir
@type envelope() :: :top_level | :cloudflare_result
```

# `name`

```elixir
@type name() ::
  :typesafe
  | :cloudflare
  | :openai
  | :openrouter
  | :gemini
  | :azure_foundry
  | :flide
  | :compatible
```

# `t`

```elixir
@type t() :: %{
  provider: name(),
  url: String.t(),
  auth: auth() | nil,
  headers: [{String.t(), String.t()}],
  model: String.t() | nil,
  envelope: envelope(),
  wire: wire()
}
```

# `wire`

```elixir
@type wire() :: :system_one | :openai_decisions | :gemini_decisions
```

# `resolve`

```elixir
@spec resolve(SystemOneClient.options()) ::
  {:ok, t()}
  | {:error,
     :missing_api_key
     | :missing_account_id
     | :missing_url
     | :missing_model
     | {:unknown_provider, term()}}
```

Resolves the endpoint, auth, default model, envelope and wire format for a request.

    iex> {:ok, p} = SystemOneClient.Provider.resolve(provider: :openai, api_key: "sk-test")
    iex> {p.url, p.model, p.wire}
    {"https://api.openai.com/v1/decisions", "gpt-6-luna", :openai_decisions}

    iex> SystemOneClient.Provider.resolve(provider: :typesafe, env: %{})
    {:error, :missing_api_key}

    iex> {:ok, p} = SystemOneClient.Provider.resolve(provider: :gemini, api_key: "g-test")
    iex> {p.model, p.wire, p.auth}
    {"gemini-decision-1", :gemini_decisions, {:header, "x-goog-api-key", "g-test"}}

# `unwrap`

```elixir
@spec unwrap(envelope(), map()) :: {:ok, map()} | {:error, term()}
```

Extracts the System One body from a provider's response envelope.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
