# FastAPI Quick overview

## Advantages:

### 1. Data validation

* Traditionally you would write code to check for values to make sure they are the correct type
* FastAPI does this for you automatically

### 2. Auto Documentation

* Can automatically generate documentation that also works kinda like a test script

### 3. Auto Completion and Code Suggestions

## Installation:

### pip:

```python
pip3 install fastapi
pip3 install uvicorn
```

### on Linux:

```python
pip install "fastapi[all]"
pip install "uvicorn[standard]"
```

### import:

```python
import fastapi
```

## What is an endpoint?

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FzCTxBX53eo8lWgPQUZac%2Fimage.png?alt=media\&token=dccf2173-99a0-411a-978d-c0fedbf9bd78)

#### Example:

* Endpoints are: `/hello` `/get-item`

You would access it by the url below if app is hosted on localhost: `localhost/hello`

### How to create an endpoint in fast-api:

* Initialize the object
* use the object followed by `.method("/endpoint-path")`
* once the endpoint is reached this will return the dictionary

#### Types of methods:

* `GET` - get info
* `POST` - send info
* `PUT` - update info
* `DELETE` - delete info

**Note:** This is similar to CRUD in SQL

### How to run the server:

```bash
cd ~/path/to/project/location.py

# Note: do not add the file extension
uvicorn location:<app name defined in py file> --reload

# Example:
uvicorn first-fastapi-api:app --reload
```

## Testing the API

You can access the link: <http://127.0.0.1:8000/docs>

This will bring you to the automatically generated API documentation

It documents all API endpoints and you can test them out as well by doing the following:

1. Select the API Endpoint and click Try it Out:&#x20;

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FtE7aqIcEDcoDks3MEWJZ%2Fimage.png?alt=media\&token=e461a742-4985-4429-bb09-97ce866329f0)

2\. Click Execute:&#x20;

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FNqVLNBWrgr86VotM89iI%2Fimage.png?alt=media\&token=21b30976-e978-40a4-90ea-cb2511a04a53)

3\. Check the Response Body, or other data that you require:&#x20;

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2Fdy8GzjCvXAgmyJTP0gYz%2Fimage.png?alt=media\&token=0df84256-6e11-474b-b6b5-672298b6543d)

## Endpoint paths

1. Generate the invetory
2. Define path of api with GET method
3. Define the get\_item function and specify what type of data is expected
4. Return data

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FAY1drPZEBW5S2gFnIxtv%2Fimage.png?alt=media\&token=ef1589ae-a63e-43a2-9ce5-ef96d3f21015)

* Returns:&#x20;

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2F50k9VAiUQnYfM4y0UdUI%2Fimage.png?alt=media\&token=a65e30aa-09bd-4875-a21e-cd8162224cf8)

* If you use ID 2 and it is not defined:&#x20;

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FGx0UI5Bz8xl7KqaonsFN%2Fimage.png?alt=media\&token=2c885653-3bb0-41da-a63b-3513a2480b98)

* If you use anything else than integer as defined:&#x20;

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FrV74s2VfIGE6qlV4U511%2Fimage.png?alt=media\&token=7efcbd55-760d-43b7-9bbf-7f19c235c886)

## Import Path

* used to detail endpoints out
* usually provided to end users

Example:&#x20;

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2F5zgdsyGRpZAVeoUUv1dl%2Fimage.png?alt=media\&token=0362f093-eefe-406b-a528-372054e8b4c9)

1. Import the `Path` variable
2. First argument has to be default (In our case `None`)
3. Added description of item
4. Go to the /docs endpoint and now the endpoint should be documented:&#x20;

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FOpSIZwDa7S6vvA2BQDKQ%2Fimage.png?alt=media\&token=22c47714-3bbf-4ff5-9c47-9237b9d661b9)
