hi there serdar yagelalp here for infoworld at idg in this episode of smart python we're going
to take a look at the basics of fast api a web framework for creating json apis using python
web apis typically work by having a client send a request formatted as json or the javascript object
notation to some endpoint or url the endpoint then returns some response also formatted as
json and various frameworks exist to support this on the server side written in various languages
in python one of the easiest and fastest frameworks to develop web apis in is named
or appropriately enough fast api fast in terms of development time since you can put together
a basic api in fast api in minutes and fast in terms of execution speed since it can make use
of python's async fixtures to be responsive fast api also uses the swagger or open api standards
so that apis generated in a fast api site can be automatically discovered let's start with a simple
fast api site that has two endpoints the root of the site which returns a static response and an
endpoint that returns a response that's dynamic it's based on some variable submitted in the url
the way we define endpoints in fast api is similar to how routes are defined in other web frameworks
like flask for each route or endpoint we write a function that handles the route's
behavior then we wrap that in a decorator that describes the route's url and which optionally
passes variables to the function or supplies other metadata about how it should be handled
for the root of the site we're not going to do anything other than return a static data set to
do this we just create a python dictionary which is automatically serialized into json by fast api
and this is really the heart of how fast api works you just create python objects like dictionaries
and they're automatically translated into json by fast api when you return them to the client
now the second endpoint for this site is a little more complex it takes in a variable from
the url which we'll call username and uses that to retrieve a variable from a dictionary in practice
this process could involve anything this could be a call out to a database for instance all that
matters is that what we get back can be serialized as json and return to the client that way now if
we pass along as username a name that already exists in the dictionary that we've defined
then we'll get back the data under that name but if we pass along something that's not there
then we will use the response variable that we've passed in here to change the response's status
code and to return some kind of useful error information in the return json as you can see
using response is entirely optional if we don't pass response into the route function
fast api will just set same defaults for how the response should be handled we only pass it in
if we need to ever make changes for instance in this case if we want to set an error code
another convenience fast api provides us with is automatic documentation for every api
if you navigate to docs in a fast api site you get a page automatically generated by
the swagger ui utility that lists all the api endpoints available in the site
along with interfaces that let you use them interactively and likewise if you navigate
to redoc there's an alternative api documentation page that's auto-generated there for you as well
fast api also makes use of python's advanced type hinting features by way of the pidantic
library one major way this is used in fast api is to create data models for what can be submitted
to or returned from an endpoint now in this example i've created a class called movie and
i used pedantic to type into each element in the class we have a name for the movie as a string
its release date as an integer an optional integer based rating and a list of strings that are tags
for the film and by default this is an empty list the first endpoint created here movie slash create
takes in some json data which we then try to coerce into the format described by the movie
class this data then gets stored in a variable new movie and we then store that in a dictionary using
the name of the movie as a key we can then use the auto-generated fast api swagger ui
to submit data to that endpoint i have here some valid pre-generated data that
we can use for that purpose so we can just copy it into the interface and submit it
and when submitted we get back a json response that tells us that everything is okay now if we
try to submit json data that doesn't conform to the standard laid down in the movie object
it will get rejected with an error so here we have some sample data that is badly formatted
and when we submit it the json in the response body gives us details about what's wrong and why
the best part about all this is that we don't have to write any code to do this validation
it happens more or less automatically because of how fast api and pedantic work together
all we have to do is define the way the data is supposed to look
the second endpoint on this site by the way it looks for a previously saved key by name the movie
name that we submitted and then we'll send that send back that information to the client as json
and likewise as before if there is no matching key in the dictionary it will return an error instead
and alter the http status appropriately and what should be clear with all this is how quickly
one can put together an api with fast api and how much of the process is almost completely automatic
fast api can also be used to build out a more full-blown site as you would with flask
or django but its core utility is in standing up a maximally useful api with minimal effort
that's it for this episode of smart python if you liked it please leave a comment below and
don't forget to subscribe to the idg tech talk channel on youtube and for more smart python
and more dev with serdar be sure to follow me on facebook youtube and infoworld.com
you