Create An API Schema Definition Using Swagger

Will Button
InstructorWill Button

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 4 months ago

Schema definitions allow you to define the format and types of data sent and received by your API. This allows consumers of your API to be confident in using your API. In this lesson, you will learn how to create a schema definition for the Todo API server.

Instructor: [0:00] We're going to use this to build our own ToDo API. We're going to get rid of some of this sample application data, starting with the definition. We're going to start by providing our own schema definition. We're going to call it a ToDo and it's going to be a type of object.

[0:20] The properties that it has will be a ToDo ID and it's going to be a type of integer. We can provide a description for that as well. The next property will be the ToDo itself. It'll be a type of string.

[0:41] We'll have a date created and it's going to be a type of, believe it or not, string. Then we're going to specify a format for that, that is the date-time format. Because this is an API, it's going to be serializing the data sent down the wire to the client, so there's no such thing as an actual date-time type.

[1:01] We use the type of string and it's going to do the format check to verify that it's the right format. We have an offer that's going to be type of string. We also have a due date. Again, it'll have that format for date-time. Then finally, we'll have a completed. It's going to be a type of Boolean.

[1:26] You'll notice as I've been typing, there have been tons of red Xs going on here on the left because it's doing validation of the YAML file as I'm typing it. Right now we've typed out a bunch of stuff that it doesn't have a reference to.

[1:42] The main thing being that this hello endpoint was using the schema of the hello world schema that I deleted, which throws everything off.

[1:52] Down here at the bottom in the model section, you can see that the ToDo that I've defined, as well as the data types and all the properties available within it. When you hit the little drop down, it gives you the description for it as well.

[2:05] As a consumer of this API, it makes for a pretty nice interface to let you know what kind of data you can expect from the API server.