I Used Python to Turn API Data Into a CSV File
APIs sounded much more complicated to me before I actually started using them.
You see words like endpoint, request, response, JSON, and authentication, and suddenly something as simple as getting a few pieces of data feels like it requires a computer science degree.
It doesn't.
One of the easiest ways to understand APIs is to forget the terminology for a moment and do something practical: request some data and save it into a CSV file.
That's what we're going to do here.
What are we actually building?
The workflow is surprisingly small:
API → JSON response → Python → CSV file
Python sends a request to an API. The API returns structured data, usually JSON. We pick the fields we want and write them into a CSV file.
That's it.
For this example, I'm using Python and the requests library.
First, install it if you haven't already:
pip install requests
Then we can make a basic request:
import requests
url = "YOUR_API_ENDPOINT"
response = requests.get(url)
print(response.status_code)
print(response.json())
If the request succeeds, response.json() converts the JSON response into Python objects that are much easier to work with.
Looking at the JSON before doing anything else
This is a step I used to skip.
Bad idea.
Before trying to save anything, print the response and actually look at its structure:
data = response.json()
print(data)
You might receive something conceptually similar to this:
{
"symbol": "ABCXYZ",
"price": "123.45",
"time": 1780000000
}
Real APIs are often more complicated and may contain nested objects or arrays.
But the principle doesn't change.
You simply need to figure out where the information you want lives inside the response.
Using a real API as the data source
For experiments like this, I prefer working with real public data instead of inventing sample values.
One source I've used is the BYDFi API documentation, which provides endpoints for retrieving market-related information.
The interesting part here isn't trading itself. It's the structure of the data.
A live API gives us something that changes over time, which makes the CSV exercise much more useful than repeatedly saving "hello world".
When working with any API, though, I always check its documentation first. Endpoint parameters, response formats, and request requirements can change.
Saving the response as CSV
Once we've extracted the fields we need, Python's built-in csv module can handle the rest.
import csv
rows = [
["symbol", "price"],
["ABCXYZ", "123.45"],
["DEFXYZ", "67.89"]
]
with open("market_data.csv", "w", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerows(rows)
Run the script and you'll get:
market_data.csv
Open it in Excel or another spreadsheet application and you'll see two simple columns: symbol and price.
Nothing revolutionary happened.
And that's exactly why I like this exercise.
You requested information from somewhere on the internet, processed it with Python, and turned it into a file that another application can understand.
You've basically built a tiny data pipeline.
Making it slightly more useful
Once the basic version works, you can start adding things.
For example, add a timestamp:
from datetime import datetime
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
Now each row can record when the data was collected.
You could also run the script periodically and append new rows instead of overwriting the existing file.
After a while, your CSV becomes a small historical dataset.
That's where this stops feeling like a programming exercise and starts becoming genuinely useful.
A few mistakes worth avoiding
The biggest one is assuming every successful HTTP request contains usable data.
A 200 response doesn't automatically mean the JSON contains exactly what you expected.
Before processing the response, check things like:
if response.status_code == 200:
data = response.json()
else:
print("Request failed:", response.status_code)
It's also worth checking whether expected keys actually exist before accessing them.
APIs change. Connections fail. Fields disappear.
Your script should expect the internet to occasionally behave like the internet.
Final thoughts
What I like about this little project is that it connects several concepts that beginners often learn separately.
HTTP requests suddenly have a purpose. JSON stops looking mysterious. Python dictionaries make more sense. CSV becomes more than something you download from a website.
And APIs stop feeling like some invisible machinery reserved for professional developers.
You're simply asking another system for structured information and deciding what to do with the answer.
Once that clicks, there are dozens of directions you can take the same idea: weather tracking, public datasets, website monitoring, financial data, analytics dashboards, or your own personal automation projects.
Sometimes the best way to understand an API is simply to make it give you a spreadsheet.
All rights reserved