Exploring APIs with Python: Accessing Data from FruitDevice

Access data from an API using Python to retrieve structured information.

Gain clarity on APIs and learn how Python can efficiently retrieve data from web resources. Understand the basics of interacting programmatically with APIs to support your data projects.

Key Insights

  • An Application Programming Interface (API) enables software developers to programmatically request and receive specific data from web resources, similar to how users interact with graphical user interfaces (GUIs).
  • The article demonstrates API functionality through FruitDevice, a simplified example where sending an HTTP request to a specified URL yields structured data about various fruits formatted as Python dictionaries and lists.
  • APIs provide essential data access points widely used in data science, machine learning, and data visualization projects, making them valuable tools for developers and data professionals.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

Hello. The first thing we're going to try to access data using Python is an API. First, I'd like to talk a little bit about what an API is and what we'll be doing with it.

So, API stands for Application Programming Interface. Let's just write that down. Application Programming Interface is kind of like a graphical user interface, which you might have heard referred to as a GUI.

And a GUI is, you know, what you're looking at right now. This is a graphical interface we're looking at here in our Python notebook. And a graphical interface is an interface that you can describe by saying, okay, when the user moves the cursor in a certain way or over a certain element—maybe I put my cursor over this to press execute—I expect a certain behavior to happen.

I expect that if I understand this interface enough, when I execute this, I know what will happen—namely, that it will execute that code. Now, an Application Programming Interface is very similar. It's an interface that we can describe where, when the user does something, they get a certain response.

However, in our case, the user—rather than someone we typically think of as a user, like someone with a mouse and a keyboard, or perhaps a touchscreen and a finger—is someone who is writing code to access data. So let's take a look at an example to understand a little more. Here we have an API called FruitDevice.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

And it's just a very simple API for getting information about fruit. If we go to FruitDevice’s home, it features some nice cheery cherries there. You can receive interesting data from any fruit of your choosing.

Sounds great. How does it work? You can receive data for a specific fruit or for all fruits, and you can add your own data. Okay.

Let's try it. What we do is send a request to a specific URL. Again, this is like in a graphical interface where clicking a certain element causes something to happen.

I expect, you know, when I click on this tab in my browser, it switches to that tab. That tab becomes visible and the previous tab goes away. If I click on this, the other tab goes away and this tab comes back.

That's what I expect to happen. Here, instead, I'm sending an HTTP—an internet request—to this URL, or rather this URL on FruitDevice.com. And if I hit return here, I get a response. And the response is something that looks a lot like Python.

This is some valid Python code here—a dictionary in a list. In fact, we have several dictionaries.

And each one appears to be all the information about one fruit. So what we're doing with this interface, instead of moving things graphically, is writing code to make a request. In this case, we’re simply programming it to send a request to a specific API—a specific URL.

And what we expect to happen is that we get data. And that's very, very helpful to those of us who work with data. Those of us doing data science, machine learning, data visualization, or any of those fields—we want data.

And APIs are a great place to get it. Okay. In our next step, we're going to get set up with accessing our first API.

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram