Skip to content

Commit 52e6adb

Browse files
Wauplinclaude
andauthored
[CLI] Add hf auth token command (#4104)
* [CLI] Add `hf auth token` command to print current token Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Add hint about `hf auth whoami` after printing token Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * update test --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent bca525e commit 52e6adb

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

docs/source/en/package_reference/cli.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ $ hf auth [OPTIONS] COMMAND [ARGS]...
6767
* `login`: Login using a token from...
6868
* `logout`: Logout from a specific token.
6969
* `switch`: Switch between access tokens.
70+
* `token`: Print the current access token to stdout.
7071
* `whoami`: Find out which huggingface.co account you...
7172

7273
### `hf auth list`
@@ -168,6 +169,29 @@ Learn more
168169
Read the documentation at https://huggingface.co/docs/huggingface_hub/en/guides/cli
169170

170171

172+
### `hf auth token`
173+
174+
Print the current access token to stdout.
175+
176+
**Usage**:
177+
178+
```console
179+
$ hf auth token [OPTIONS]
180+
```
181+
182+
**Options**:
183+
184+
* `--help`: Show this message and exit.
185+
186+
Examples
187+
$ hf auth token
188+
$ hf auth token | xargs curl -H 'Authorization: Bearer {}'
189+
190+
Learn more
191+
Use `hf <command> --help` for more information about a command.
192+
Read the documentation at https://huggingface.co/docs/huggingface_hub/en/guides/cli
193+
194+
171195
### `hf auth whoami`
172196

173197
Find out which huggingface.co account you are logged in as.

src/huggingface_hub/cli/auth.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ def auth_list_cmd() -> None:
148148
auth_list()
149149

150150

151+
@auth_cli.command("token", examples=["hf auth token", "hf auth token | xargs curl -H 'Authorization: Bearer {}'"])
152+
def auth_token() -> None:
153+
"""Print the current access token to stdout."""
154+
token = get_token()
155+
if token is None:
156+
out.error("Not logged in. Run `hf auth login` first.")
157+
raise typer.Exit(code=1)
158+
print(token)
159+
out.hint("Run `hf auth whoami` to see which account this token belongs to.")
160+
161+
151162
@auth_cli.command("whoami", examples=["hf auth whoami", "hf auth whoami --format json"])
152163
def auth_whoami(
153164
format: FormatWithAutoOpt = OutputFormatWithAuto.auto,

tests/test_cli.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
)
3333
from huggingface_hub.utils._verification import FolderVerification
3434

35+
from .testing_constants import TOKEN
3536
from .testing_utils import DUMMY_MODEL_ID, with_production_testing
3637

3738

@@ -1658,6 +1659,22 @@ def test_whoami_not_logged_in_json(self, runner: CliRunner) -> None:
16581659
assert "Not logged in" in result.output
16591660

16601661

1662+
class TestAuthTokenCommand:
1663+
def test_token_prints_to_stdout(self, runner: CliRunner) -> None:
1664+
with patch("huggingface_hub.cli.auth.get_token", return_value=TOKEN):
1665+
result = runner.invoke(app, ["auth", "token"])
1666+
assert result.exit_code == 0
1667+
assert result.stdout.strip() == TOKEN
1668+
assert "hf auth whoami" in result.output
1669+
1670+
def test_token_not_logged_in(self, runner: CliRunner) -> None:
1671+
with patch("huggingface_hub.cli.auth.get_token", return_value=None):
1672+
result = runner.invoke(app, ["auth", "token"])
1673+
assert result.exit_code == 1
1674+
assert "Not logged in" in result.output
1675+
assert "hf auth login" in result.output
1676+
1677+
16611678
class TestModelsLsCommand:
16621679
def test_models_ls_basic(self, runner: CliRunner) -> None:
16631680
repo = ModelInfo(

0 commit comments

Comments
 (0)