Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 983 of the free egghead.io lessons, plus get Node.js content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Create an API with Swagger

3:56 Node.js lesson by

Swagger is a project used to describe restful APIs using the OpenAPI Specification. It allows you to document your API so consumers understand the endpoints, parameters, and responses. In this lesson, I'll show you how to install the swagger command line tool, create a new API project using swagger, and introduce you to the swagger API editor.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Swagger is a project used to describe restful APIs using the OpenAPI Specification. It allows you to document your API so consumers understand the endpoints, parameters, and responses. In this lesson, I'll show you how to install the swagger command line tool, create a new API project using swagger, and introduce you to the swagger API editor.

Avatar
Marcel

Hi, first I want to say that I like Egg-head. In many cases, you are ahead, and all your teachers are very smart. You guys put me on the right track in deciding what matters and whats good. Reasons enough to be very happy that you guys are around. Following the lessons is something else. Egg-head style lessons are famous to me because in most cases they leave me dazzeled after the first 3 minutes in lesson 1.

I, as a simple student, am lost again after the first 3 minutes Swagger. because there is no context. In this case: where can I find what is required in Swagger so that when I see the lesson, I can put things in context.
Example: at 1:45 in lesson 1, you say: I want to get rid of the licence stuff. Why would you want to do get rid of the licence stuff. Where can I find the Swagger documentation so that I understand this decison. At 2:06, it turns out property 'path' is missing. But first you want to add the 'host' element. Why would you first want to add the 'host' element. And how would I know a 'host' element is required in the first place.
If I, as a simple student, want to get to know Swagger, I will need to find a course (like I did for Angular2) that goes 1 step at a time. But thanks for drawing my attention to Swagger.

Marcel

In reply to egghead.io
Avatar
David

I think there is something wrong with this lesson: there is no instructions on how to install the command line, neither an introduction to Swagger. However, in the RSS appeared a different lesson with the same title that shows you how to install the command line and give a better introduction than this lesson.

Avatar
Will

Hey David,
You're absolutely right! That's not the correct lesson. We're getting the correct one uploaded now. I'll update here when it's fixed. Thanks for pointing it out!

In reply to David
Avatar
Will

Hey Marcel,
Thanks for your feedback. It's really important for us to hear what is working and what is not working for you. I completely understand your frustration, it sounds very familiar to what I experienced when first introduced to swagger.
There are a couple of things at work here:
First: this is the wrong lesson. It's an earlier version of this course I created, but wasn't happy with so I scrapped it and started over from scratch. The corrected version is getting uploaded as I type this.
Second: The swagger documentation can be found at http://swagger.io/specification/ but I think you'll find the same frustration there, I know I did, and that's what led me to creating this course. There are a lot of interdependencies in a swagger spec, including some that don't seem to make sense until much later. To deal with this, I tried to create a course that shows the 'how-to' steps so that you can use swagger immediately and be successful, then return to the finer points later as needed.
Thanks again for your feedback, it's extremely helpful in delivering the courses and lessons that you want!

In reply to Marcel
Avatar
Will

Hi David,
The correct lesson has been uploaded. Thanks again for pointing it out!

In reply to David
Avatar
Will

Marcel,
The correct lesson has been uploaded. Thanks for your feedback and support!

In reply to Marcel
Avatar
Marcel

Hi Will,
Thanks for taking my comment as I meant it: feedback that might help improve. I look forward to your new course, diving into swagger.

In reply to Will
Avatar
Samir

And this time it's great: clear and easy to follow lesson :) I used swagger at work on existing projects (so just consuming APIs) and didn't know it was that easy to create an API with it. Great news :)

In reply to Will
Avatar
Will

That's great to hear!
Thanks!

In reply to Samir
Avatar
Olivers De Abreu

Hi great tutorial, but I have the following question. When you are deploying to production how do you set the host and port variables?

For example in development you have in your yaml file http and localhost:10010 as your protocol, host and port, how do I change this to https and myapi.com ?

Do I have to use a nginx proxy server? It's there a way to do this with NODE_ENV variables?

Thank you

Avatar
Will

Hey Olivers,
For production: always use a proxy, either nginx as you mentioned or an ELB if you are using AWS.
This allows you to load balance across multiple node.js instances, the SSL overhead can be handled by the proxy so you don't have to do it inside your node application, and you don't have to figure out how to get node to run as root so it can bind to the ports 80 and 443.

In reply to Olivers De Abreu
Avatar
Olivers De Abreu

Great thank you

In reply to Will

To get started using Swagger, the first thing I need to do is install the package itself. It has a command line interface, I'm going to install it using the -g flag to install it globally.

With that installed, I can create a new Swagger project using the Swagger command with Swagger project create, and then the name for my project, which is going to be todo-api.

It supports multiple frameworks, including Express, Hapi, Restify, and Sales. I'm going to use the Express framework, and that's going to kick off the installation from the skeleton project. Switching over to my other pane, I can do a directory listing. There's a new directory called "todo-api."

I can go into that directory, and then start my project using the Swagger project, start, and the name of my project. It gives us an example that we can use here to start it, we'll just try that over here in our other pane. It returns, "Hello Scott."

The really cool part about using Swagger is that it has this editor that allows us to define our API. We can get to that using the Swagger project edit command. When I run that command, it opens up a browser for me, points it to localhost, and to a port that it's created to host this Swagger interface.

Let's start looking on the right-hand side first. You can see that it has the name of our application, the version number, and then, it defines all of the paths that exist in the API. We've already used the /hello endpoint in our curl command that we did earlier, but it also allows us to use that from this editor, as well.

It gives us a description about the endpoint, and tells us about all the parameters, a description, where it's located at, the type of data that it is. Same thing with the responses, and it gives you the details of the response that you can expect back from the API.

We also have the ability to try it out from within the editor. It selects the scheme for us. Then, we can change the content type if we're selecting multiple content types. Then parameters, we can supply. You can see, as I supplied that parameter, it updated the URL that will be called with this.

Then I can send that response, or send that request. It comes back with a success message. I can choose the output, and it shows the output from the API server. It's pretty cool. It allows you to test your API without actually having to have a specific client to do that.

All of that comes from this definition over here on the left-hand side. Starting off, we've got the Swagger that defines the version of Swagger we're using. You've already seen the version number and the title that were displayed over here on the left, and the host name, as well, where the server's going to be running from.

The most useful part of this is in this path section, where you'll define each of the endpoints hosted by your API. You can see one, the /hello endpoint that we used a minute ago. You can see how the description gets mapped over here to the UI provided to us.

Other things to note here are the operation ID, which is the name of the function that's going to be called within your application to respond to this endpoint request. The types of responses that you support is defined.

Then, there's this concept of definitions down here, where you can define a schema for the response itself. That's going to allow you to do some type validation on the responses that your API server is sending back down to the client so that as a consumer of this API, you can be confident that the data you're getting matches to the schema specification.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?