# `HuggingfaceClient.Plug`
[🔗](https://github.com/huggingface/huggingface_client/blob/v0.1.0/lib/huggingface_client/plug.ex#L1)

Optional Plug middleware for Phoenix / Plug applications.

Injects a pre-configured `HuggingfaceClient.Client` into the `conn` assigns
so controllers don't have to create a client on every request.

## Usage in Phoenix

    # In your endpoint or router:
    plug HuggingfaceClient.Plug,
      token_from: {:env, "HF_TOKEN"},
      assign_as: :hf_client,
      provider: "groq"

    # In a controller:
    def generate(conn, params) do
      {:ok, resp} = HuggingfaceClient.chat_completion(conn.assigns.hf_client, %{
        model: "meta-llama/Llama-3.1-8B-Instruct",
        messages: [%{role: "user", content: params["prompt"]}]
      })
      json(conn, resp)
    end

## Token sources

The `:token_from` option controls where the access token is read:

- `{:env, "VAR_NAME"}` — read from OS environment at startup (default: `{"env", "HF_TOKEN"}`)
- `{:config, :app, :key}` — read from `Application.get_env/2`
- `{:static, "hf_..."}` — hardcoded value (testing only)
- `:conn_assign` — read from `conn.assigns[assign_as <> "_token"]`
- `nil` — no token (public models only)

---

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