Authentication and authorization for HuggingFace Hub.
Handles token management, login/logout flows, and user information retrieval.
Examples
# Get current token
{:ok, token} = HfHub.Auth.get_token()
# Set token
:ok = HfHub.Auth.set_token("hf_...")
# Get current user info
{:ok, user} = HfHub.Auth.whoami()
# Logout
:ok = HfHub.Auth.logout()
Summary
Functions
Builds HTTP authorization headers from the current or provided token.
Retrieves the current HuggingFace token.
Interactive login flow.
Logs out and removes stored credentials.
Sets the HuggingFace authentication token.
Validates a token.
Gets information about the current authenticated user.
Types
Functions
Builds HTTP authorization headers from the current or provided token.
Options
:token- Token to use. If not provided, uses get_token/0.
Examples
{:ok, headers} = HfHub.Auth.auth_headers()
# => {:ok, [{"authorization", "Bearer hf_..."}]}
{:ok, headers} = HfHub.Auth.auth_headers(token: "hf_custom")
@spec get_token() :: {:ok, String.t()} | {:error, :no_token}
Retrieves the current HuggingFace token.
Checks in order:
- Application configuration
- HF_TOKEN environment variable
- Stored credentials file
Examples
{:ok, token} = HfHub.Auth.get_token()
Interactive login flow.
Prompts for a token and stores it for future use.
Options
:token- Token to use (skips prompt):add_to_git_credentials- Add token to git credentials. Defaults tofalse.
Examples
:ok = HfHub.Auth.login(token: "hf_...")
:ok = HfHub.Auth.login() # Interactive prompt
@spec logout() :: :ok
Logs out and removes stored credentials.
Examples
:ok = HfHub.Auth.logout()
@spec set_token(String.t()) :: :ok
Sets the HuggingFace authentication token.
The token is stored in application configuration for the current session.
Arguments
token- HuggingFace API token (starts with "hf_")
Examples
:ok = HfHub.Auth.set_token("hf_...")
Validates a token.
Checks if the token is properly formatted and valid with the Hub API.
Arguments
token- Token to validate
Examples
:ok = HfHub.Auth.validate_token("hf_...")
{:error, :invalid_token} = HfHub.Auth.validate_token("bad_token")
Gets information about the current authenticated user.
Requires a valid authentication token.
Examples
{:ok, user} = HfHub.Auth.whoami()
IO.inspect(user.username)
IO.inspect(user.organizations)