Authentication and runtime configuration

Copy Markdown View Source

hf_hub_ex is a library: it does not read operating-system environment variables directly from runtime modules. The host application owns that boundary and passes configuration through options or application config.

For an application or script, read secrets in config/runtime.exs or an equivalent config provider:

import Config

if token = System.get_env("HF_TOKEN") do
  config :hf_hub, token: token
end

cache_dir = System.get_env("HF_HUB_CACHE") || System.get_env("HF_HOME")
if cache_dir, do: config(:hf_hub, cache_dir: cache_dir)

case System.get_env("HF_HUB_OFFLINE") do
  value when value in ["1", "true", "TRUE", "yes", "YES"] ->
    config :hf_hub, offline: true

  _ ->
    :ok
end

For one-off IEx usage, passing token: explicitly is also fine:

token = System.fetch_env!("HF_TOKEN")
{:ok, repo} = HfHub.Repo.create("my-org/my-dataset", repo_type: :dataset, token: token)

Library defaults

Local development

Use your preferred shell secret manager (direnv, a private wrapper script, or your deployment platform's secret manager) to set HF_TOKEN before starting the host app. Do not commit tokens to config files.