Blog > Develop RESTful API with FastAPI

Card image cap

Develop RESTful API with FastAPI

  • Jan. 5, 2023, 6:44 p.m.

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python-type hints. One of FastAPI's main goals is to be easy to use and learn. It has a number of features that make it very convenient to use and allow developers to build APIs more quickly and with less code.

Let's get to some introduction about REST and RESTful API.

REST (Representational State Transfer) is a software architectural style that defines a set of constraints to be used for creating web services. RESTful APIs are APIs that follow the REST architectural style and guidelines.

Some of the key principles of REST include:

  • Client-server architecture: The client (e.g., a web browser) sends a request to the server (e.g., a web server), and the server returns a response.

  • Statelessness: The server does not store any state about the client sessions in between requests. All the information needed to handle a request is included in the request itself.

  • Cacheability: RESTful APIs should be designed to be cacheable, meaning that clients can cache the responses to improve performance.

  • Layered system: A RESTful API can be used by multiple clients, such as web browsers, mobile apps, or other servers. The API itself is not aware of the specific client that is using it.

RESTful APIs are often used to build web services that are lightweight, maintainable, and scalable. They are widely used in modern web development and are a key part of the API (Application Programming Interface) ecosystem.

Based on the introduction about REST, RESTful API, and FastAPI above let's go deep into how to create RESTful API using FastAPI. We are going to create an API to add and get users using FastAPI.

To create an API to add and get users using FastAPI, we need to follow these steps:

  1. 1.Install FastAPI and the required dependencies:  

    Let's get to know about the above dependencies:
    1. FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. One of FastAPI's main goals is to be easy to use and learn. It has a number of features that make it very convenient to use and allow developers to build APIs more quickly and with less code
    2. Uvicorn is a lightning-fast ASGI server, built on top of uvloop and httptools. It is designed to be easy to use and provides a number of features that make it well-suited for use in developing web APIs.
    3. Pydantic is a library for data validation and parsing in Python. It is designed to be easy to use, easy to learn, and fast. Pydantic is built on top of the Python 3.6+ type hints and provides a number of features that make it very convenient for use in web development, including automatic data validation and conversion, and support for defining and validating complex data structures.
  2. 2. Define a model for the user data using pydantic. This model will define the structure of the user data and also provide automatic validation of the data:
  3. 3. Create an API endpoint to add a new user. To do this, we can define a function that takes the user data as input and stores it in a database or file, but based on our code below we will store the data in the list called Users as shown below:
  4. 4. Create an API endpoint to retrieve users. To do this, we can define a function that retrieves Users data from the database or file:
    All of the above code in one file (main.py) is as follows:
  5. 5.Run the API using uvicorn,  we can run the following command in the terminal:

After Running the application, we need to navigate to http://127.0.0.1:8000/docs to view our APIs, and will be shown as follows:

All of Two APIs:

The POST API to create a User:

The GET API to get a list of added Users:

 

You can get all of the work above from the GitHub link Sample FastAPI APIs.

For any suggestions leave them in the comment area below,  Thanks.

 

Blog Comments

Comments

123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('m',2)='m

123456 on: Jan. 18, 2024, 6:30 p.m.

123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('o',0)='o

123456 on: Jan. 18, 2024, 6:30 p.m.

123456/**/and/**/3=DBMS_PIPE.RECEIVE_MESSAGE('a',2)

123456 on: Jan. 18, 2024, 6:30 p.m.

123456/**/and/**/2=DBMS_PIPE.RECEIVE_MESSAGE('g',0)

123456 on: Jan. 18, 2024, 6:30 p.m.

123456'and(select+1)>0waitfor/**/delay'0:0:2

123456 on: Jan. 18, 2024, 6:30 p.m.

123456'and(select+1)>0waitfor/**/delay'0:0:0

123456 on: Jan. 18, 2024, 6:30 p.m.

123456/**/and(select+1)>0waitfor/**/delay'0:0:2'/**/

123456 on: Jan. 18, 2024, 6:30 p.m.

123456/**/and(select+1)>0waitfor/**/delay'0:0:0'/**/

123456 on: Jan. 18, 2024, 6:30 p.m.

123456'/**/and(select'1'from/**/pg_sleep(2))::text>'0

123456 on: Jan. 18, 2024, 6:30 p.m.

123456'/**/and(select'1'from/**/pg_sleep(0))::text>'0

123456 on: Jan. 18, 2024, 6:30 p.m.

123456/**/and(select+1/**/from/**/pg_sleep(2))>0/**/

123456 on: Jan. 18, 2024, 6:30 p.m.

123456/**/and(select+1/**/from/**/pg_sleep(0))>0/**/

123456 on: Jan. 18, 2024, 6:30 p.m.

123456"and(select*from(select+sleep(2))a/**/union/**/select+1)="

123456 on: Jan. 18, 2024, 6:30 p.m.

123456"and(select*from(select+sleep(0))a/**/union/**/select+1)="

123456 on: Jan. 18, 2024, 6:30 p.m.

123456'and(select*from(select+sleep(2))a/**/union/**/select+1)='

123456 on: Jan. 18, 2024, 6:30 p.m.

123456'and(select*from(select+sleep(0))a/**/union/**/select+1)='

123456 on: Jan. 18, 2024, 6:30 p.m.

(select*from(select+sleep(2)union/**/select+1)a)

123456 on: Jan. 18, 2024, 6:30 p.m.

(select*from(select+sleep(0)union/**/select+1)a)

123456 on: Jan. 18, 2024, 6:30 p.m.

123456"and"b"="l

123456 on: Jan. 18, 2024, 6:30 p.m.

123456"and"d"="d

123456 on: Jan. 18, 2024, 6:30 p.m.

123456'and'b'='g

123456 on: Jan. 18, 2024, 6:30 p.m.

123456'and'k'='k

123456 on: Jan. 18, 2024, 6:30 p.m.

123456/**/and+3=6

123456 on: Jan. 18, 2024, 6:30 p.m.

123456/**/and+3=3

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('a',2)='a on: Jan. 18, 2024, 6:30 p.m.

123456

123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('x',0)='x on: Jan. 18, 2024, 6:30 p.m.

123456

123456/**/and/**/4=DBMS_PIPE.RECEIVE_MESSAGE('s',2) on: Jan. 18, 2024, 6:30 p.m.

123456

123456/**/and/**/4=DBMS_PIPE.RECEIVE_MESSAGE('o',0) on: Jan. 18, 2024, 6:30 p.m.

123456

123456'and(select+1)>0waitfor/**/delay'0:0:2 on: Jan. 18, 2024, 6:30 p.m.

123456

123456'and(select+1)>0waitfor/**/delay'0:0:0 on: Jan. 18, 2024, 6:30 p.m.

123456

123456/**/and(select+1)>0waitfor/**/delay'0:0:2'/**/ on: Jan. 18, 2024, 6:30 p.m.

123456

123456/**/and(select+1)>0waitfor/**/delay'0:0:0'/**/ on: Jan. 18, 2024, 6:30 p.m.

123456

123456'/**/and(select'1'from/**/pg_sleep(2))::text>'0 on: Jan. 18, 2024, 6:30 p.m.

123456

123456'/**/and(select'1'from/**/pg_sleep(0))::text>'0 on: Jan. 18, 2024, 6:30 p.m.

123456'"\(

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456/**/and(select+1/**/from/**/pg_sleep(2))>0/**/ on: Jan. 18, 2024, 6:30 p.m.

123456鎈'"\(

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456/**/and(select+1/**/from/**/pg_sleep(0))>0/**/ on: Jan. 18, 2024, 6:30 p.m.

123456'and/**/convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1793883807')))>'0

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456"and(select*from(select+sleep(2))a/**/union/**/select+1)=" on: Jan. 18, 2024, 6:30 p.m.

convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1284042233')))

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456"and(select*from(select+sleep(0))a/**/union/**/select+1)=" on: Jan. 18, 2024, 6:30 p.m.

123456/**/and/**/cast(md5('1088428902')as/**/int)>0

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456'and(select*from(select+sleep(2))a/**/union/**/select+1)=' on: Jan. 18, 2024, 6:30 p.m.

123456'and(select'1'from/**/cast(md5(1179765920)as/**/int))>'0

123456 on: Jan. 18, 2024, 6:30 p.m.

<%- 984022361+870539030 %>

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456'and(select*from(select+sleep(0))a/**/union/**/select+1)=' on: Jan. 18, 2024, 6:30 p.m.

extractvalue(1,concat(char(126),md5(1292753711)))

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

(select*from(select+sleep(2)union/**/select+1)a) on: Jan. 18, 2024, 6:30 p.m.

#set($c=974801372+946116530)${c}$c

123456 on: Jan. 18, 2024, 6:30 p.m.

123456"and/**/extractvalue(1,concat(char(126),md5(1440662276)))and"

123456 on: Jan. 18, 2024, 6:30 p.m.

${(935743866+970968311)?c}

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

(select*from(select+sleep(0)union/**/select+1)a) on: Jan. 18, 2024, 6:30 p.m.

123456'and/**/extractvalue(1,concat(char(126),md5(1583733221)))and'

123456 on: Jan. 18, 2024, 6:30 p.m.

${932823767+892691203}

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456 on: Jan. 18, 2024, 6:30 p.m.

expr 967009043 + 905316495

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456'"\( on: Jan. 18, 2024, 6:30 p.m.

/*1*/{{888178987+860859995}}

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456 on: Jan. 18, 2024, 6:30 p.m.

123456&set /A 865899647+838971647

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456鎈'"\( on: Jan. 18, 2024, 6:30 p.m.

123456

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

<%- 827009222+822378500 %> on: Jan. 18, 2024, 6:30 p.m.

123456$(expr 839325197 + 892779207)

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456'and/**/convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1159165169')))>'0 on: Jan. 18, 2024, 6:30 p.m.

123456

123456"and"l"="v on: Jan. 18, 2024, 6:30 p.m.

123456

#set($c=908963393+833695869)${c}$c on: Jan. 18, 2024, 6:30 p.m.

123456|expr 938822207 + 971926842

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1877789839'))) on: Jan. 18, 2024, 6:30 p.m.

123456

123456"and"g"="g on: Jan. 18, 2024, 6:30 p.m.

123456

${(847520730+886387421)?c} on: Jan. 18, 2024, 6:30 p.m.

123456 expr 824893637 + 852856608

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456/**/and/**/cast(md5('1643555326')as/**/int)>0 on: Jan. 18, 2024, 6:30 p.m.

123456

123456'and'o'='h on: Jan. 18, 2024, 6:30 p.m.

123456

${856926104+920691074} on: Jan. 18, 2024, 6:30 p.m.

123456

expr 854966652 + 862823348 on: Jan. 18, 2024, 6:30 p.m.

123456

123456'and(select'1'from/**/cast(md5(1460207004)as/**/int))>'0 on: Jan. 18, 2024, 6:30 p.m.

123456

123456'and'q'='q on: Jan. 18, 2024, 6:30 p.m.

123456

/*1*/{{818282904+982533588}} on: Jan. 18, 2024, 6:30 p.m.

123456

123456&set /A 949539166+845543092 on: Jan. 18, 2024, 6:30 p.m.

123456

extractvalue(1,concat(char(126),md5(1579655423))) on: Jan. 18, 2024, 6:30 p.m.

123456

'-var_dump(md5(568699386))-' on: Jan. 18, 2024, 6:30 p.m.

123456

123456/**/and+2=7 on: Jan. 18, 2024, 6:30 p.m.

123456

123456$(expr 800666722 + 983419629) on: Jan. 18, 2024, 6:30 p.m.

123456

123456"and/**/extractvalue(1,concat(char(126),md5(1733183061)))and" on: Jan. 18, 2024, 6:30 p.m.

123456

${@var_dump(md5(102166998))}; on: Jan. 18, 2024, 6:30 p.m.

123456

${810757889+861138418} on: Jan. 18, 2024, 6:30 p.m.

123456

123456/**/and+2=2 on: Jan. 18, 2024, 6:30 p.m.

123456

123456|expr 901972544 + 991248491 on: Jan. 18, 2024, 6:30 p.m.

123456

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456'and/**/extractvalue(1,concat(char(126),md5(1113778954)))and' on: Jan. 18, 2024, 6:30 p.m.

123456

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456 expr 983318917 + 950561122 on: Jan. 18, 2024, 6:30 p.m.

123456

123456 on: Jan. 18, 2024, 6:30 p.m.

${811743020+841994564}

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456 on: Jan. 18, 2024, 6:30 p.m.

'-var_dump(md5(321101647))-'

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456 on: Jan. 18, 2024, 6:30 p.m.

${@var_dump(md5(302072321))};

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456 on: Jan. 18, 2024, 6:30 p.m.

123456

123456 on: Jan. 18, 2024, 6:30 p.m.

That is good bro

Einstein Jr on: Jan. 6, 2023, 2:54 p.m.

Hats off πŸ‘

Kutina on: Jan. 6, 2023, 12:52 p.m.

Bravo πŸ‘πŸ‘

Arnold Alphonce on: Jan. 6, 2023, 12:12 p.m.

Good Stuff.

Godfrey Temu on: Jan. 6, 2023, 11:47 a.m.

keep it up,

Jb on: Jan. 6, 2023, 7:42 a.m.

This is so nice, the blog post explains it all. Congrats πŸ‘πŸΎπŸ‘πŸΎπŸ‘πŸΎ

Larry Matrix on: Jan. 5, 2023, 9:35 p.m.

Thanks @Abdul for the Questions, the next article will cover Capability of FastAPI more over I will show an examples of APIs that includes Database connection with ORM (Tortoise ORM), I hope you will learn more on that article

Ismaili Mohamedi on: Jan. 5, 2023, 9:24 p.m.

Also would like you to expand on Object Relational Mapping pydantic is using, and the database thats syncing with model defined in the example.

Abdul Makuya on: Jan. 5, 2023, 9:09 p.m.

πŸ‘πŸ½

Mariam Juma on: Jan. 5, 2023, 8:56 p.m.