> For the complete documentation index, see [llms.txt](https://docs.arkannis.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.arkannis.net/programming/python/frameworks/fastapi/fastapi-quick-overview.md).

# 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?

![](/files/W5CFOI4d9iafAPxntzzM)

#### 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;

![](/files/UdYRrfuZA9opVmgdKcsX)

2\. Click Execute:&#x20;

![](/files/OXvMSms4uPC4z3bisvWn)

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

![](/files/o1ihbUlYhJFXFjGCPlJo)

## 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

![](/files/qu2fdRMtefvzzRJFMTvS)

* Returns:&#x20;

![](/files/whzHvV0yoD50YOXZYMFO)

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

![](/files/NtKMQnDDkyNhlrPjDrzN)

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

![](/files/C35ybv77dSgBv68JDyKf)

## Import Path

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

Example:&#x20;

![](/files/dBeURLW6cvyKKxSMrQ0z)

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;

![](/files/AGDh47bQ9ZPcxurJ4ko4)
