The one idea
An API call is just your Python script making an HTTP request and using the response. Treat it like a file read that can fail because the network, URL, key, or server may be wrong.
What you need
Most API work in Python uses the requests package. It is not part of the Python standard library, so it must go into requirements.txt.
For hackathon projects, use a real API if you have one. If keys or credits are an issue, use a mock API while keeping the same code shape.
Install requests
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 4 done
pip install requests
pip freeze > requirements.txt
python -c "import requests; print(requests.__version__)"Call a mock API
This example reads a city name from code for now, sends it as a query parameter, checks the response, and writes selected fields to output.json.
It uses httpbin.org, a public HTTP testing service. Later you can replace the URL with your AI API, weather API, startup data API, or college project backend.
import json
import requests
url = "https://httpbin.org/get"
params = {"city": "Bengaluru", "task": "demo"}
headers = {"User-Agent": "hackindia-python-tool/1.0"}
try:
response = requests.get(url, params=params, headers=headers, timeout=10)
response.raise_for_status()
except requests.RequestException as error:
print(f"API call failed: {error}")
raise SystemExit(1)
data = response.json()
result = {
"called_url": data["url"],
"sent_params": data["args"],
"status": "ok"
}
with open("output.json", "w", encoding="utf-8") as file:
json.dump(result, file, indent=2)
print("Wrote output.json")Keep secrets out
Do not paste API keys into your script or commit them to GitHub. Read them from environment variables.
If you use ChatGPT or Gemini to generate API code, ask it to use environment variables and timeout handling. Then check that it did not hard-code keys, skip error handling, or write unclear output.
import os
api_key = os.getenv("MY_API_KEY")
if not api_key:
raise SystemExit("Missing MY_API_KEY")
headers = {"Authorization": f"Bearer {api_key}"}Common API mistakes
Beginners often print the full response and stop there. Your proof needs useful output: a JSON, CSV, or text file that another person can inspect.
Another common mistake is ignoring status codes. A failed API can still return text, but your script should detect the failure and exit clearly.
response.raise_for_status()Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done