Private Space authentication for external API calls

Hello everyone!
I’m using a Docker :hugs: Space to deploy my FastAPI application that uses multiple models, but I’ve set it to private since my project contains sensitive code. My problem is that I can’t send requests to the endpoints from anywhere outside my browser and get a 404.

Is it possible to send a :hugs: token with the request to authenticate myself? If so, how should I include it in my request to make it work properly?

Thank you all in advance! :hand_with_fingers_splayed:

1 Like

If the space is functioning properly, you should be able to access it like following.
You can figure out the actual space URL yourself, also you can also find it using the GUI.

curl -X POST https://OWNER-SPACENAME.hf.space/api/predict \
  -H "Authorization: Bearer $HF_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"text":"hello"}'

or

import os, requests
url = "https://OWNER-SPACENAME.hf.space/api/predict"
r = requests.post(url,
                  headers={"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"},
                  json={"text": "hello"},
                  timeout=60)
print(r.status_code, r.text)

If you want to implement more complex access control.

1 Like

yup it worked, thank youu!
my problem was with the token

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.