- Saved searches
- Use saved searches to filter your results more quickly
- wpcodevo/python_fastapi
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- readMe.md
- About
- Saved searches
- Use saved searches to filter your results more quickly
- mecomontes/FastAPI
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
This article will teach you how to create a CRUD RESTful API with Python, FastAPI, SQLAlchemy ORM, Pydantic, Alembic, PostgreSQL, and Docker-compose to perform the basic Create/Read/Update/Delete operations against a database.
wpcodevo/python_fastapi
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
readMe.md
RESTful API with Python, FastAPI, Pydantic, SQLAlchemy and Docker
1. RESTful API with Python,SQLAlchemy, & FastAPI: Access and Refresh Tokens
In this article, you’ll learn how to secure a FastAPI app by implementing access and refresh token functionalities using JSON Web Tokens (JWTs). We’ll use the FastAPI JWT Auth package to sign, encode and decode the access and refresh JWT tokens.
- Python FastAPI JWT Authentication Overview
- How to Setup FastAPI with PostgreSQL
- Setup FastAPI
- Initialize a Simple FastAPI Server
- User Registration Controller
- User Sign-in Controller
- Refresh Access Token Controller
- Logout User Controller
2. RESTful API with Python, SQLAlchemy, & FastAPI: Send HTML Emails
In this article, you’ll learn how to send HTML emails with Python, FastAPI, SQLAlchemy, PostgreSQL, Jinja2, and Docker-compose. Also, you’ll learn how to dynamically generate HTML templates with the Jinja2 package.
- Send HTML Email with jinja2 and FastAPI Overview
- Creating an SMTP Provider Account
- Validating the Environment Variables with Pydantic
- Create a Database Model with Sqlalchemy
- Creating the HTML Email Templates with Jinja2
- Set up SMTP Email Sender
- How to Send the HTML Email
- Update the SignUp Path Operation Function
- Create a Controller to Verify the Code
3. CRUD RESTful API Server with Python, SQLAlchemy, FastAPI, and PostgreSQL
This article will teach you how to create a CRUD RESTful API with Python, FastAPI, SQLAlchemy ORM, Pydantic, Alembic, PostgreSQL, and Docker-compose to perform the basic Create/Read/Update/Delete operations against a database.
- Python, FastAPI, PostgreSQL, SQLAlchemy CRUD API Overview
- Setting up FastAPI and PostgreSQL
- Building the FastAPI Server
- Starting the FastAPI Server
- Fetch All Posts Handler
- Create New Post Handler
- Update Post Handler
- Get a Single Post Handler
- Remove Post Handler
About
This article will teach you how to create a CRUD RESTful API with Python, FastAPI, SQLAlchemy ORM, Pydantic, Alembic, PostgreSQL, and Docker-compose to perform the basic Create/Read/Update/Delete operations against a database.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. The key features are: Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available. Fast to code: Increase the speed to develop features b…
mecomontes/FastAPI
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
- Easy to get started 😊
- Great for building Rest APIs and microservices.
- Used by big companies like Netflix, Reddit, Lyft, Et cetera.
- Great for building APIS for machine learning applications.
- Force is strong with this one 😉
- Beginners with basic Python knowledge who wants to build cool apps.
- Experienced flask developers who have been working with flask SSR (Server Side Rendering).
First of all install pipenv
Pipenv is used to create a virtual environment which isolates the python packages you used in this project from other system python packages. So, that you can have a different version of same python packages for different projects.
Now, let’s install flask using pipenv
This will create a new virtual environment and install flask. This command will create two files Pipfile and Pipfile.lock .
Pipfile contains the packages that are required for your application. As you can see flask is added to [packages] list. This means when someone downloads your code and runs pipenv install , flask gets installed in their system. Great, right?
If you are familiar with requirements.txt , think Pipfile as the requirements.txt on steroids.
Flask is so simple that you can create an API using a single file. (But you don’t have to 😅 )
Create a new file called app.py where we are gonna write our Flask Hello World API. Write the following code in app.py
Companion application to my RESTful Authentication with Flask article.
After cloning, create a virtual environment and install the requirements. For Linux and Mac users:
$ virtualenv venv $ source venv/bin/activate (venv) $ pip install -r requirements.txt
If you are on Windows, then use the following commands instead:
$ virtualenv venv $ venv\Scripts\activate (venv) $ pip install -r requirements.txt
To run the server use the following command:
(venv) $ python api.py * Running on http://127.0.0.1:5000/ * Restarting with reloader
Then from a different terminal window you can send requests.
- POST /api/users Register a new user.
The body must contain a JSON object that defines username and password fields.
On success a status code 201 is returned. The body of the response contains a JSON object with the newly added user. A Location header contains the URI of the new user.
On failure status code 400 (bad request) is returned.
Notes:- The password is hashed before it is stored in the database. Once hashed, the original password is discarded.
- In a production deployment secure HTTP must be used to protect the password in transit.
The following curl command registers a new user with username miguel and password python :
$ curl -i -X POST -H "Content-Type: application/json" -d '' http://127.0.0.1:5000/api/users HTTP/1.0 201 CREATED Content-Type: application/json Content-Length: 27 Location: http://127.0.0.1:5000/api/users/1 Server: Werkzeug/0.9.4 Python/2.7.3 Date: Thu, 28 Nov 2013 19:56:39 GMT
These credentials can now be used to access protected resources:
$ curl -u miguel:python -i -X GET http://127.0.0.1:5000/api/resource HTTP/1.0 200 OK Content-Type: application/json Content-Length: 30 Server: Werkzeug/0.9.4 Python/2.7.3 Date: Thu, 28 Nov 2013 20:02:25 GMT
Using the wrong credentials the request is refused:
$ curl -u miguel:ruby -i -X GET http://127.0.0.1:5000/api/resource HTTP/1.0 401 UNAUTHORIZED Content-Type: text/html; charset=utf-8 Content-Length: 19 WWW-Authenticate: Basic realm="Authentication Required" Server: Werkzeug/0.9.4 Python/2.7.3 Date: Thu, 28 Nov 2013 20:03:18 GMT Unauthorized Access
Finally, to avoid sending username and password with every request an authentication token can be requested:
$ curl -u miguel:python -i -X GET http://127.0.0.1:5000/api/token HTTP/1.0 200 OK Content-Type: application/json Content-Length: 139 Server: Werkzeug/0.9.4 Python/2.7.3 Date: Thu, 28 Nov 2013 20:04:15 GMT
And now during the token validity period there is no need to send username and password to authenticate anymore:
$ curl -u eyJhbGciOiJIUzI1NiIsImV4cCI6MTM4NTY2OTY1NSwiaWF0IjoxMzg1NjY5MDU1fQ.eyJpZCI6MX0.XbOEFJkhjHJ5uRINh2JA1BPzXjSohKYDRT472wGOvjc:x -i -X GET http://127.0.0.1:5000/api/resource HTTP/1.0 200 OK Content-Type: application/json Content-Length: 30 Server: Werkzeug/0.9.4 Python/2.7.3 Date: Thu, 28 Nov 2013 20:05:08 GMT
Once the token expires it cannot be used anymore and the client needs to request a new one. Note that in this last example the password is arbitrarily set to x , since the password isn’t used for token authentication.
An interesting side effect of this implementation is that it is possible to use an unexpired token as authentication to request a new token that extends the expiration time. This effectively allows the client to change from one token to the next and never need to send username and password after the initial token was obtained.
v0.3 — Return token duration.
v0.2 — Return a 201 status code and Location header from /api/users endpoint.
v0.1 — Initial release.