nsidnev
fastapi-realworld-example-app
Python

Backend logic implementation for https://github.com/gothinkster/realworld with awesome FastAPI

Last updated Jul 10, 2026
3.1k
Stars
724
Forks
0
Issues
0
Stars/day
Attention Score
54
Language breakdown
Python 98.4%
Shell 0.9%
Mako 0.4%
Dockerfile 0.4%
โ–ธ Files click to expand
README

.. image:: ./.github/assets/logo.png

|

.. image:: https://github.com/nsidnev/fastapi-realworld-example-app/workflows/API%20spec/badge.svg :target: https://github.com/nsidnev/fastapi-realworld-example-app

.. image:: https://github.com/nsidnev/fastapi-realworld-example-app/workflows/Tests/badge.svg :target: https://github.com/nsidnev/fastapi-realworld-example-app

.. image:: https://github.com/nsidnev/fastapi-realworld-example-app/workflows/Styles/badge.svg :target: https://github.com/nsidnev/fastapi-realworld-example-app

.. image:: https://codecov.io/gh/nsidnev/fastapi-realworld-example-app/branch/master/graph/badge.svg :target: https://codecov.io/gh/nsidnev/fastapi-realworld-example-app

.. image:: https://img.shields.io/github/license/Naereen/StrapDown.js.svg :target: https://github.com/nsidnev/fastapi-realworld-example-app/blob/master/LICENSE

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg :target: https://github.com/ambv/black

.. image:: https://img.shields.io/badge/style-wemake-000000.svg :target: https://github.com/wemake-services/wemake-python-styleguide


NOTE: This repository is not actively maintained because this example is quite complete and does its primary goal - passing Conduit testsuite.

More modern and relevant examples can be found in other repositories with `fastapi tag on GitHub.

Quickstart


First, run PostgreSQL, set environment variables and create database. For example using docker: ::

export POSTGRESDB=rwdb POSTGRESPORT=5432 POSTGRESUSER=postgres POSTGRESPASSWORD=postgres docker run --name pgdb --rm -e POSTGRESUSER="$POSTGRESUSER" -e POSTGRESPASSWORD="$POSTGRESPASSWORD" -e POSTGRESDB="$POSTGRESDB" postgres export POSTGRES_HOST=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' pgdb) createdb --host=$POSTGRESHOST --port=$POSTGRESPORT --username=$POSTGRESUSER $POSTGRESDB

Then run the following commands to bootstrap your environment with poetry: ::

git clone https://github.com/nsidnev/fastapi-realworld-example-app cd fastapi-realworld-example-app poetry install poetry shell

Then create .env file (or rename and modify .env.example) in project root and set environment variables for application: ::

touch .env echo APP_ENV=dev >> .env echo DATABASEURL=postgresql://$POSTGRESUSER:$POSTGRESPASSWORD@$POSTGRESHOST:$POSTGRESPORT/$POSTGRESDB >> .env echo SECRET_KEY=$(openssl rand -hex 32) >> .env

To run the web application in debug use::

alembic upgrade head uvicorn app.main:app --reload

If you run into the following error in your docker container:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Ensure the DATABASE_URL variable is set correctly in the .env file. It is most likely caused by POSTGRES_HOST not pointing to its localhost.

DATABASE_URL=postgresql://postgres:postgres@0.0.0.0:5432/rwdb

Run tests


Tests for this project are defined in the tests/ folder.

Set up environment variable DATABASEURL or set up databaseurl in app/core/settings/test.py

This project uses pytest _ to define tests because it allows you to use the assert keyword with good formatting for failed assertations.

To run all the tests of a project, simply run the pytest command: ::

$ pytest ================================================= test session starts ================================================== platform linux -- Python 3.8.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir: /home/some-user/user-projects/fastapi-realworld-example-app, inifile: setup.cfg, testpaths: tests plugins: env-0.6.2, cov-2.9.0, asyncio-0.12.0 collected 90 items

tests/testapi/testerrors/test422error.py . [ 1%] tests/testapi/testerrors/test_error.py . [ 2%] tests/testapi/testroutes/test_articles.py ................................. [ 38%] tests/testapi/testroutes/test_authentication.py .. [ 41%] tests/testapi/testroutes/test_comments.py .... [ 45%] tests/testapi/testroutes/test_login.py ... [ 48%] tests/testapi/testroutes/test_profiles.py ............ [ 62%] tests/testapi/testroutes/test_registration.py ... [ 65%] tests/testapi/testroutes/test_tags.py .. [ 67%] tests/testapi/testroutes/test_users.py .................... [ 90%] tests/testdb/testqueries/test_tables.py ... [ 93%] tests/testschemas/testrw_model.py . [ 94%] tests/testservices/testjwt.py ..... [100%]

============================================ 90 passed in 70.50s (0:01:10) ============================================= $

If you want to run a specific test, you can do this with this _ pytest feature: ::

$ pytest tests/testapi/testroutes/testusers.py::testusercannottakealreadyusedcredentials

Deployment with Docker


You must have docker and docker-compose tools installed to work with material in this section. First, create .env file like in Quickstart section or modify .env.example. POSTGRES_HOST must be specified as db or modified in docker-compose.yml also. Then just run::

docker-compose up -d db docker-compose up -d app

Application will be available on localhost in your browser.

Web routes


All routes are available on /docs or /redoc paths with Swagger or ReDoc.

Project structure


Files related to application are in the app or tests` directories. Application parts are:

::

app โ”œโ”€โ”€ api - web related stuff. โ”‚ย ย  โ”œโ”€โ”€ dependencies - dependencies for routes definition. โ”‚ย ย  โ”œโ”€โ”€ errors - definition of error handlers. โ”‚ย ย  โ””โ”€โ”€ routes - web routes. โ”œโ”€โ”€ core - application configuration, startup events, logging. โ”œโ”€โ”€ db - db related stuff. โ”‚ย ย  โ”œโ”€โ”€ migrations - manually written alembic migrations. โ”‚ย ย  โ””โ”€โ”€ repositories - all crud stuff. โ”œโ”€โ”€ models - pydantic models for this application. โ”‚ย ย  โ”œโ”€โ”€ domain - main models that are used almost everywhere. โ”‚ย ย  โ””โ”€โ”€ schemas - schemas for using in web routes. โ”œโ”€โ”€ resources - strings that are used in web responses. โ”œโ”€โ”€ services - logic that is not just crud related. โ””โ”€โ”€ main.py - FastAPI application creation and configuration.

ยฉ 2026 GitRepoTrend ยท nsidnev/fastapi-realworld-example-app ยท Updated daily from GitHub