Check out a free preview of the full Go for JavaScript Developers course:
The "External APIs" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna introduces the section on getting data from an external API by visiting the website for the Star Wars API, the API that will be used for the rest of this section. After hitting one of the API endpoints on the site, students are instructed to create a basic route.

Get Unlimited Access Now

Transcript from the "External APIs" Lesson

[00:00:00]
>> So the next thing we're going to do is going to be kind of a similar exercise. But instead of creating to do's through a forum, we're going to hit an external API and then parse that request using a couple of the JSON related functions that come with go.

[00:00:10] So we're going to hop over to exercise 12a. But the first thing I'd like to do is talk about the API we're actually going to use. So I'm going to be working with the HTTPSswapi.co file, which is the Star Wars API, which allows you to hit an endpoint and get a bunch of Star Wars data back.

[00:00:31] And the interesting thing about this particular API which we will look at right now. Is it has some cool nested structures to it that make it interesting to work with, especially when talking about HTTP requests, so. The Star Wars API homepage, so it's all of the data you've ever wanted around Star Wars related things.

[00:00:58] So the input we're gonna hit is going to be this base URL, which is swapi.co/api. And we're going to start with just getting all of the people. So one of the endpoints that exists is /people and you'll see some examples here. And when you hit request, you'll see the results come back with an array of objects in JSON here.

[00:01:21] That each have name, height, hair color, etc. And then their films actually is an array of additional endpoints. They have a list of starships, a list of vehicles, a list of species. What's important to note before we get into the code is that the endpoint is going to be /people.

[00:01:41] That's where we're going to start. Cool, so let's navigate over to our code and we're in section 12. API and API.go. So what currently exist in this file is simply the beginning of that base URL, and if I kill my server. And let's go ahead and run section 12 server Earth 12_api/code/api.go.

[00:02:08] You'll see it simply just printing out that base server. It's kind of our starting point. Cool. So let's start by adding a route to /people. I'm going to give you about five minutes to build this on your own. So once that route exists, we're going to navigate or fire off the function to display people on the screen.

[00:02:26] And I'd like you to use the F print function to have something actually render to the browser when you see that /people. So create a route for people and then write a function that uses fnt.Fprint that indicates the request was successful. Just take one to two minutes to fire off those routes and you can use the last exercise as an example.