# Gradio API. Again. WebSocket connection: HTTP 403

**URL:** <https://discuss.huggingface.co/t/gradio-api-again-websocket-connection-http-403/144416>\
**Category:** Spaces\
**Created:** [March 6, 2025, 10:58am UTC](https://discuss.huggingface.co/t/gradio-api-again-websocket-connection-http-403/144416 "2025-03-06T10:58:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkmanika](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@dkmanika](https://discuss.huggingface.co/u/dkmanika)\
**Post date:** [March 6, 2025, 10:58am UTC](https://discuss.huggingface.co/t/gradio-api-again-websocket-connection-http-403/144416/1 "2025-03-06T10:58:35Z")

</div>

Hello.  
Gradio space, public. Chat-bot works OK. Requests via Bash - OK.  
But when trying to connect by API from Telegram-bot: server rejected WebSocket connection: HTTP 403.  
On Space, requerements(parts):  
huggingface-hub\>=0.19.3  
gradio==5.1.0  
fastapi==0.112.2  
gradio-client\>=1.3.0  
urllib3~=2.0

On hosting, requirements:  
psutil  
huggingface-hub\>=0.19.3  
openpyxl==3.1.2  
pandas\>=2.1.0  
transformers==4.35.0  
torch\>=1.13.1  
tokenizers\>=0.15.0,\<1.0  
pymorphy2\>=0.8  
regex\>=2023.10.3  
python-telegram-bot==20.2  
httpx\>=0.24.1

gradio-client\>=1.3.0.  
fastapi==0.112.2

python-dotenv==1.0.0  
requests\>=2.28.2  
urllib3~=2.0  
python-telegram-bot\>=20.2  
aiohttp\>=3.8.5  
async-timeout==4.0.2

What else should i add in Requiremens or why API does not work?  
Help pls! 2+ weeks trying to fix…

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [March 6, 2025, 2:59pm UTC](https://discuss.huggingface.co/t/gradio-api-again-websocket-connection-http-403/144416/2 "2025-03-06T14:59:24Z")

</div>

The most common cause of connection failures from Gradio clients is a mismatch between the Gradio space and the Gradio client version.

> <https://github.com/h2oai/h2ogpt/issues/1691>
>
> I encountered an issue while using the Gradio client to connect to a Gradio appl…ication running at \`http://localhost:7860/\`. The client initially reports successful loading of the API, but attempts to establish a WebSocket connection are rejected by the server with an HTTP 403 (Forbidden) status code.
> 
> \*\*Error Message:\*\*
> 
> \`\`\`
> Loaded as API: http://localhost:7860/ ✔
> GR job failed: server rejected WebSocket connection: HTTP 403
> File "/path/to/miniconda3/envs/gradioclient/lib/python3.10/concurrent/futures/thread.py", line 58, in run
> result = self.fn(\*self.args, \*\*self.kwargs)
> File "/path/to/miniconda3/envs/gradioclient/lib/python3.10/site-packages/gradio\_client/client.py", line 798, in \_inner
> predictions = \_predict(\*data)
> File "/path/to/miniconda3/envs/gradioclient/lib/python3.10/site-packages/gradio\_client/client.py", line 827, in \_predict
> result = utils.synchronize\_async(self.\_ws\_fn, data, hash\_data, helper)
> File "/path/to/miniconda3/envs/gradioclient/lib/python3.10/site-packages/gradio\_client/utils.py", line 540, in synchronize\_async
> return fsspec.asyn.sync(fsspec.asyn.get\_loop(), func, \*args, \*\*kwargs) # type: ignore
> File "/path/to/miniconda3/envs/gradioclient/lib/python3.10/site-packages/fsspec/asyn.py", line 103, in sync
> raise return\_result
> File "/path/to/miniconda3/envs/gradioclient/lib/python3.10/site-packages/fsspec/asyn.py", line 56, in \_runner
> result\[0\] = await coro
> File "/path/to/miniconda3/envs/gradioclient/lib/python3.10/site-packages/gradio\_client/client.py", line 1055, in \_ws\_fn
> async with websockets.connect( # type: ignore
> File "/path/to/miniconda3/envs/gradioclient/lib/python3.10/site-packages/websockets/legacy/client.py", line 637, in \_\_aenter\_\_
> return await self
> File "/path/to/miniconda3/envs/gradioclient/lib/python3.10/site-packages/websockets/legacy/client.py", line 655, in \_\_await\_impl\_timeout\_\_
> return await self.\_\_await\_impl\_\_()
> File "/path/to/miniconda3/envs/gradioclient/lib/python3.10/site-packages/websockets/legacy/client.py", line 662, in \_\_await\_impl\_\_
> await protocol.handshake(
> File "/path/to/miniconda3/envs/gradioclient/lib/python3.10/site-packages/websockets/legacy/client.py", line 329, in handshake
> raise InvalidStatusCode(status\_code, response\_headers)
> 
> Loaded as API: http://localhost:7860/ ✔
> GR job failed again: server rejected WebSocket connection: HTTP 403
> ... (same stack trace as above)
> \`\`\`
> 
> \*\*Steps to Reproduce:\*\*
> 
> 1. python generate.py --base\_model=TheBloke/Mistral-7B-Instruct-v0.2-GGUF --prompt\_type=mistral --max\_seq\_len=4096
> 2. Run the Gradio client code. 
> \`\`\`
> import time
> import os
> import sys
> 
> from gradio\_utils.grclient import GradioClient
> 
> \# self-contained example used for readme, to be copied to README\_CLIENT.md if changed, setting local\_server = True at first
> \# The grclient.py file can be copied from h2ogpt repo and used with local gradio\_client for example use
> 
> if local\_server:
> client = GradioClient("http://0.0.0.0:7860")
> else:
> h2ogpt\_key = os.getenv('H2OGPT\_KEY') or os.getenv('H2OGPT\_H2OGPT\_KEY')
> if h2ogpt\_key is None:
> sys.exit("API key not found. Exiting.")
> # if you have API key for public instance:
> client = GradioClient("https://gpt.h2o.ai", h2ogpt\_key=h2ogpt\_key)
> 
> 
> print(client.question("Who are you?"))
> \`\`\`
> 
> \* \*\*Environment:\*\*
> \* Operating System: Ubuntu 22.04.4 LTS
> \* gradio: 4.26.0
> \* gradio\_client: 0.6.1
>     
>     
> \* \*\*Note:\*\*
> I suspect this is due to internal network restrictions and using 127.0.0.1 should ideally work. Unfortunately it doesn't.
