Guide to creating a React + FastAPI full stack application

Posted by


Building a full-stack application with React and FastAPI can be a powerful combination, allowing you to create a dynamic front-end interface with React and a robust back-end server with FastAPI. In this tutorial, we will guide you through the process of building a React + FastAPI application from scratch.

Prerequisites

Before getting started, make sure you have the following tools and technologies installed on your system:

  1. Node.js and npm (Node Package Manager) for building the front-end with React.
  2. Python and pip for building the back-end with FastAPI.
  3. Visual Studio Code or any other code editor of your choice.
  4. Git for version control.

Step 1: Set Up the Back-End with FastAPI

  1. Create a new directory for your project and navigate to it in the terminal.

  2. Run the following command to create a new virtual environment for your FastAPI project:

    python -m venv venv
  3. Activate the virtual environment with the following command:

    source venv/bin/activate
  4. Install FastAPI and Uvicorn (ASGI server) using pip:

    pip install fastapi uvicorn
  5. Create a new Python file for your FastAPI application, e.g., main.py, and import FastAPI:
    
    from fastapi import FastAPI

app = FastAPI()


6. Define a sample endpoint for testing:
```python
@app.get("/")
def read_root():
    return {"message": "Hello World"}
  1. Run the FastAPI application using Uvicorn:

    uvicorn main:app --reload
  2. Visit http://localhost:8000 in your browser to see the "Hello World" message.

Step 2: Set Up the Front-End with React

  1. Create a new directory for your React project within the project directory.

  2. Initialize a new React app using create-react-app:

    npx create-react-app client
  3. Navigate to the client directory and start the React development server:

    cd client
    npm start
  4. Your React app should now be running at http://localhost:3000.

Step 3: Connect the Front-End to the Back-End

  1. Create a proxy configuration for the React app to communicate with the FastAPI server. Create a setupProxy.js file in the client/src directory and add the following code:
    
    const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function (app) {
app.use(‘/api’, createProxyMiddleware({ target: ‘http://localhost:8000‘, changeOrigin: true }));
};


2. Modify the `package.json` file in the `client` directory to add a `proxy` configuration:
```json
"proxy": "http://localhost:8000"
  1. Use the fetch API or any HTTP library of your choice in the React components to make requests to the FastAPI back-end. For example:

    fetch('/api/')
    .then(response => response.json())
    .then(data => console.log(data));
  2. Build and deploy your React app by running:
    npm run build

Conclusion

In this tutorial, we have walked you through the process of building a full-stack application with React and FastAPI. You have learned how to set up the back-end with FastAPI, set up the front-end with React, and connect the two parts to communicate with each other. Feel free to extend this project with additional features or integrate databases, authentication, or other functionalities to create a more robust full-stack application. Happy coding!

0 0 votes
Article Rating
36 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@edgo6632
3 months ago

Thanks, Eric!

@todorowael
3 months ago

Great tutorial, thank you for all your efforts!

@remimahdi233
3 months ago

Hey, nice tutorial, can we get a tutorial to put this in a production environment ? I never saw this on youtube

@robel_asefa
3 months ago

You are not really explaining anything at all. You are just copying from somewhere.

@danieldosreis1
3 months ago

10:15
😀

@paritoshkulkarni6354
3 months ago

What theme are you using here?

@goodatsomething7669
3 months ago

Thanks for this to the point and precise tutorial!

@tanmaydash803
3 months ago

in background where the data store is it in sqlite or mongodb or mysql ?????

@mohammednasser1361
3 months ago

Great video
Is there any plans for react +faspi course +

@codingwithroby
3 months ago

Here is how you deploy FastAPI + React + PostgreSQL on AWS: https://www.udemy.com/course/deploy-fastapi-fullstack-amazon-cloud-aws/?referralCode=706861EF28A616B36FEC

@rstiekema
3 months ago

Thanks a lot. The React part is totally new for me and didn't understand much, but it works haha

@felixnj
3 months ago

hello am doing thisone for my school project …….and i want to deploy it how do i do so please?

@felixnj
3 months ago

hello am doing this as my school project how do i deploy it? please

@rainerpavletic4814
3 months ago

Thank you. Super helpful. Can you do NextJS/Fastapi next time? and/or Nextjs/fastapi using vercel usechat SDK?

@liangzhang5420
3 months ago

Hi Eric, great tutorial. thank you. I am trying to build some prototype application using React+fastapi+celery+redis+mongodb, but it is going slow. by any chance, do you have some patreon channel that we can subscribe for paid membership where we can ask some technical questions? thank you. and have a nice day.

@DevlogBill
3 months ago

Just found this video and I haven't watched it yet. My focus is React and I tried implementing the backend with Django but it is so complicated with Django. Some tutorials which show how to create a React app with Django tends to be over 3hrs plus. I saw a couple of videos with fastAPI and I see the videos tend to be smaller in length for the tutorials. Is it easier to create a backend restful api with FastAPI? I am now starting the tutorial. How good is FastAPI for creating api endpoints? Thanks and I guess I will find out.

@oblivion7300
3 months ago

wait cors is included already with fastAPi.

@jessicagrant9020
3 months ago

This was a great Video! thanks!! by any change do you have a upcoming video with React + fast API with Jinja templating?

@valentinmarin3998
3 months ago

Thanks a lot Eric for this great course, excellent indeed, it's content, your broad knowledge and delivery really engage the audience, very clear and precise explanations, I highly recommend the instructor and the course, best wishes and God bless you.

@johnharrington8784
3 months ago

Thank you for making such a great video! It was very helpful, the example was relevant and it explained everything you need. Great job!