I have created a test program example to demontrate the error. It is using the python URLLIB package.
import os, json, urllib.request
URL = “https://api.groq.com/openai/v1/chat/completions”
API_KEY = os.getenv(“GROQ_API_KEY”)
HEADERS = {“Authorization”: "Bearer " + API_KEY, “Content-Type”: “application/json”}
PAYLOAD = {“model”:“openai/gpt-oss-120b”,“messages”:\[{“role”:“user”,“content”:“Hello, world!”}\],“temperature”:0.7}
body = json.dumps(PAYLOAD).encode(“utf-8”)
req = urllib.request.Request(url=URL, data=body, headers=HEADERS, method=“POST”)
resp = urllib.request.urlopen(req)
print(resp.read().decode(“utf-8”))
error:
$ python3 test.py
Traceback (most recent call last):
File "/home/alec/.config/sublime-text/Packages/Agentic/test.py", line 8, in <module>
resp = urllib.request.urlopen(req)
File "/usr/lib/python3.10/urllib/request.py", line 216, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.10/urllib/request.py", line 525, in open
response = meth(req, response)
File "/usr/lib/python3.10/urllib/request.py", line 634, in http_response
response = self.parent.error(
File "/usr/lib/python3.10/urllib/request.py", line 563, in error
return self._call_chain(*args)
File "/usr/lib/python3.10/urllib/request.py", line 496, in _call_chain
result = func(*args)
File "/usr/lib/python3.10/urllib/request.py", line 643, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
no error:
# Modify the test program to include User-Agent in HEADERS
HEADERS = {"Authorization": "Bearer " + API_KEY, "Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
$ python3 test.py
{"id":"chatcmpl-2fc09dff-2c91-4b9c-8fac-107d0d4b8e56","object":"chat.completion","created":1766449633,"model":"openai/gpt-oss-120b","choices":[{"index":0,"message":{"role":"assistant","content":"Hello! How can I assist you today?","reasoning":"The user says \"Hello, world!\" Probably just a greeting. We respond politely."},"logprobs":null,"finish_reason":"stop"}],"usage":{"queue_time":0.004695947,"prompt_tokens":75,"prompt_time":0.002884385,"completion_tokens":36,"completion_time":0.07572427,"total_tokens":111,"total_time":0.078608655,"completion_tokens_details":{"reasoning_tokens":18}},"usage_breakdown":null,"system_fingerprint":"fp_c652c0ffaa","x_groq":{"id":"req_01kd49mpksf0prvs1wzjzyj59h","seed":268796957},"service_tier":"on_demand"}