Assistance with 401 Unauthorized Error for API Access
I am currently encountering an issue while attempting to use the Hugging Face Inference API with access tokens. Despite following all documentation and testing multiple configurations, I am consistently receiving a401 Unauthorizederror when calling the/modelsendpoint.Here is a summary of the steps I have taken to troubleshoot this issue:I generated bothread-onlyandfine-grainedtokens from my account settings.I ensured the tokens include permissions for:Making calls to the serverless Inference API.Making calls to Inference Endpoints.I verified that my network settings allow the API to be reached, as confirmed by successful TLS handshakes.I tested the tokens using bothcurland Python, specifically targeting:https://api-inference.huggingface.co/modelshttps://api-inference.huggingface.co/models/distilbert-base-uncasedAll attempts resulted in the same401 Unauthorizederror, despite using valid tokens.Based on the error and my troubleshooting, I suspect the issue might be related to the limitations of a free-tier account. Could you kindly confirm:Whether a paid subscription is required to access the/modelsendpoint or specific API features?If there are other configurations or settings I might be missing to enable access with a free-tier account?I greatly appreciate your guidance and support in resolving this issue. Please let me know if additional details are needed.Thank you in advance for your help.Best regards,Miguel Crespo
2024-12-01T03:18:21Z
That’s strange, it’s working fine over here. Try this.https://api-inference.huggingface.co/models/distilbert/distilbert-base-uncased
2024-12-01T06:57:55Z
Thank you John,To be more specific, I’m getting a 401 error at my end when I try to use a token that I had created. Please see curl verbose output:curl -v -H “Authorization: Bearer REDACTED_TOKEN”https://api-inference.huggingface.co/models<HTTP/2 401< date: Sun, 01 Dec 2024 02:44:17 GMT< content-length: 0< vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers< x-request-id: -x55LlHujz9vzSw9J7-gp< access-control-allow-credentials: true<Connection#0to hostapi-inference.huggingface.coleft intactimage1380×1067 44 KBThank you!
2024-12-01T07:03:14Z
Thanks for the details. It looks like this, but the strange thing is that there shouldn’t be a problem even if you don’t specify it in particular, and it doesn’t look like an SSL error from the message content…stackoverflow.comwhat does "* Connection #0 to host example.com left intact" mean?curlasked bycooldude101on11:56PM - 18 May 20 UTCAdding a header “Connection: close” to your CURL command should solve the problem:curl -v -H “Connection: close”https://example.com
2024-12-01T20:05:40Z
[quote=“John6666, post:4, topic:128643”]message[/quote] Hi John, really appreciate you looking into this, I tried incorporating your suggestion but unfortunately I get the same error, HTTP/2 400, invalid token.I generated bothread-onlyandfine-grainedtokens from my account settings. But I always get invalid token at the HF end.All seems correct, but somehow HF says the token is invalid.Please see screenshot.Thanks for your help!image1719×1174 204 KB
2024-12-01T20:29:49Z
@not-lain@meganarileyWeird 401 problem.
2024-12-02T06:42:42Z
Hi john,Do you think this could be because I need to have a paid subscription? Do you manage to get tokens and then use them?Thank you!
2024-12-02T06:45:37Z
It’s true that there are advantages for those who subscribe to the paid plan, but I don’t think that’s the case here. If it’s because you’re a free member, I think you’d get a different error rather than a 401 error…
2024-12-03T06:42:47Z
Hi John,I don’t know if this is possible, but would you be able to run this command in your environment and see if you still get the same error?Not sure if I can ask this, if not, not to worry. I’ll try something elsecurl -v -H “Authorization: Bearer REDACTED_TOKEN” -H “Connection: close”https://api-inference.huggingface.co/modelsThank you!
2024-12-03T07:44:10Z
There’s nothing wrong with it. I’ll hide some of it, but it’s generally like this. Windows CMD.Incidentally, the result was exactly the same even if I used my own token.Host api-inference.huggingface.co:443 was resolved.

*   Trying 35.171.117.73:443...
* Connected to api-inference.huggingface.co (35.171.117.73) port 443
* schannel: disabled automatic use of client certificate
* ALPN: curl offers http/1.1
* using HTTP/1.x
> GET /models HTTP/1.1
> Host: api-inference.huggingface.co
> User-Agent: curl/8.9.1
> Accept: */*
> Authorization: Bearer hf_*******
> Connection: close
>
* Request completely sent off
* schannel: server close notification received (close_notify)
< HTTP/1.1 401 Unauthorized
< Date: Tue, 03 Dec 2024 07:41:39 GMT
< Content-Length: 0
< Connection: close
< access-control-allow-credentials: true
< x-request-id: ********
< vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers
<
* shutting down connection #0
* schannel: shutting down SSL/TLS connection with api-inference.huggingface.co port 443
2024-12-04T05:08:57Z
Hi John,Thank you so much for your help, much appreciated. I will repost the issue again on the forum and see if someone from support can help me on this.Have a great day!Miguel.