Hugging Face, End to EndLesson 36 of 75

Contents

Hugging Face, End to End

Lesson 36 of 75. Sign in to keep your place.

Module 4 · Spaces: putting something in front of people

Every Space is an API you can call

8 min read

The feature almost nobody uses

Scroll to the bottom of any Gradio Space and there is a link reading Use via API. Click it. You get the endpoint names, the parameter types, and working code in Python and JavaScript.

This is not an extra somebody configured. Gradio exposes every event handler as an endpoint automatically. Which means: any public Space is a hosted service you can call from your own code, without deploying anything, without a key beyond a Hub token, and without the model being on your machine.

Bash
pip install gradio_client
Python
from gradio_client import Client

client = Client("abidlabs/whisper")
result = client.predict("audio.wav", api_name="/predict")
print(result)

Files work too, in both directions:

Python
from gradio_client import Client, handle_file

client = Client("your-name/your-space")
out = client.predict(
    image=handle_file("https://example.com/photo.jpg"),
    threshold=0.5,
    api_name="/segment",
)

handle_file takes a local path or a URL and does the upload. The result is a path to a downloaded file in a temporary directory.

Long jobs, without blocking

Python
job = client.submit("a long prompt", api_name="/generate")
while not job.done():
    print(job.status())
print(job.result())

submit returns immediately and the job reports queue position and progress. This is what you want inside a web application, where blocking for ninety seconds is not an option.

The honest limits

Calling somebody else’s Space is convenient and it is not a service level agreement:

  • It can be deleted or changed at any moment, by someone who owes you nothing.
  • It can be asleep, so your first call waits out a cold start.
  • It is queued, behind everyone else on the internet using it.
  • Your input goes to their container, and you do not know what they log. Never send personal or confidential data to a Space you do not own. This is the same judgement you would apply to any third-party API, and the informality of the Hub makes people forget it.

The responsible pattern is to duplicate the Space first, privately, and call your copy. You then control the hardware, the sleep policy and the data, at the cost of the hardware bill. For a Space you rely on, this is not optional.

Naming your own endpoints

If you want your Space to be callable, name the endpoints:

Python
btn.click(fn=transcribe, inputs=[audio], outputs=[text], api_name="transcribe")
other.click(fn=internal_helper, inputs=[x], outputs=[y], api_name=False)

api_name="transcribe" gives a stable /transcribe route that survives layout changes. api_name=False hides a handler from the API entirely, which you want for anything that is a UI detail rather than a capability.

A Space with named endpoints and an honest readme is a small, free, public service. That is a useful thing to publish, and it costs one keyword argument.

Private Spaces need a token

A private Space, or one on gated hardware, needs authentication on every call:

Python
client = Client("your-name/private-space", hf_token=os.environ["HF_TOKEN"])

Read the token from the environment rather than writing it in the script, for the reasons the tokens lesson sets out. A fine-grained token scoped to that one repository is better than an account-wide one, because a token that leaks from a client is a token that can be revoked without breaking everything else you own.

The MCP option

Gradio can expose a Space’s functions as an MCP server, so an AI assistant that speaks the protocol can call your Space as a tool:

Python
demo.launch(mcp_server=True)

The function signature and docstring become the tool description, which means type hints and a clear docstring stop being style and start being interface. Whether you use this or not, it is worth knowing that the same Space can be a web page, a REST endpoint and a tool for an agent with no additional code.

When not to do any of this

If you are calling a Space in a loop over ten thousand items, stop. You are using somebody’s free demo hardware as a batch processor, which is rude, slow, and will be rate limited. Run the model yourself, duplicate the Space onto hardware you pay for, or use an inference provider. Spaces are for demonstration and light use; the platform’s terms say as much.

Do this now

Pick any public Space that does something you need, open Use via API, and call it from a Python script. Then decide honestly whether you should be calling theirs or duplicating it, and if the answer is duplicate, do that instead.

Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.

Check your understanding

A startup builds a product feature on gradio_client calls to a stranger’s public Space. What is the most serious problem with this, beyond reliability?

Pick the one you would defend. Your answer stays private.

Next lesson · 8 minIt built, it started, it is red

Stuck on something?Ask Arjun, your AI tutor

How to cite: Addaly (2026) ‘Every Space is an API you can call’, in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/calling-a-space-as-an-api (Accessed: 13 October 2026).