Full-Stack Setup: FastAPI + React.js + Vite + Material-UI (MUI) with TypeScript
In this tutorial, we'll create a simple full-stack application using FastAPI for the back-end, React.js (with TypeScript) and Vite for the front-end, and Material-UI (MUI) for building the user interface. This step-by-step guide will walk you through building a full-stack app using modern tooling and fast development servers.
If you’re looking for a production-ready version of this setup, we’ve built a complete boilerplate with built-in features like authentication, database integration, and more, which you can find at the end.
Table of Contents
- Project Structure Overview
- Setting Up FastAPI (Back-End)
- Setting Up React with Vite, TypeScript, and Material-UI (Front-End)
- Connecting React to FastAPI (API Calls)
- Adding Material-UI Components
- Running and Testing the App
1. Project Structure Overview
We’ll follow a structure similar to our CraftYourStartup Boilerplate to keep everything organized:
my-fullstack-app/
│
├── backend/ # FastAPI app (Back-End)
│ ├── app/
│ │ └── main.py # Main FastAPI app logic
│ └── poetry.lock
│ └── pyproject.toml # Poetry dependency file
│
└── frontend/ # React.js (TypeScript) Front-End with Vite
├── public/
├── src/
│ ├── App.tsx # React app main file in TypeScript
├── package.json
└── vite.config.ts # Vite configuration for dev and prod
The backend folder will contain the FastAPI app, while the frontend folder holds the React + TypeScript code built with Vite.
2. Setting Up FastAPI (Back-End)
We'll start by setting up the FastAPI back-end, which will serve as the API for the React front-end.
Steps:
-
Create the Back-End Directory:
-
Install Poetry: We’re using Poetry to manage dependencies:
-
Install FastAPI and Uvicorn: Install the necessary dependencies:
-
Create a Simple FastAPI App: In
backend/app/main.py
:
-
Run the FastAPI Server: Start the server:
Your API will be live at
http://localhost:8000
.
3. Setting Up React with Vite, TypeScript, and Material-UI (Front-End)
Now, let’s set up the React front-end using Vite, TypeScript, and Material-UI.
Steps:
-
Create the Front-End Directory:
-
Install Dependencies: Install Material-UI components and Axios for making HTTP requests:
-
Update Vite Configuration for Proxy: Update
vite.config.ts
to proxy requests to the FastAPI back-end during development:
-
Update the App Component: In
src/App.tsx
, update it to make an API call to your FastAPI back-end:
import React, { useEffect, useState } from "react"; import axios from "axios"; import { Container, Typography, Button } from "@mui/material"; const App: React.FC = () => { const [message, setMessage] = useState<string>(""); useEffect(() => { axios.get("/api/") .then(response => setMessage(response.data.message)) .catch(error => console.error("Error fetching data", error)); }, []); return ( <Container> <Typography variant="h3" gutterBottom> FastAPI + React + Vite + MUI (TypeScript) </Typography> <Typography variant="body1"> {message || "Loading..."} </Typography> <Button variant="contained" color="primary" style={{ marginTop: '20px' }}> Material UI Button </Button> </Container> ); }; export default App;
4. Connecting React to FastAPI (API Calls)
To connect the front-end and back-end, we use Axios for making API requests. Vite's proxy configuration will forward /api
requests to the FastAPI back-end.
-
Make API Requests: In the
useEffect
block ofApp.tsx
, we're making a request to the FastAPI server by calling the/api/
endpoint: -
Test the Connection: Make sure your React app is successfully calling the FastAPI back-end by running both servers (FastAPI and Vite) simultaneously.
5. Adding Material-UI Components
With the front-end and back-end connected, let's add some Material-UI components to enhance the UI.
- Adding a TextField:
You can add a TextField component to capture input from users. Replace the button with the following in
App.tsx
:
import { TextField } from "@mui/material"; const App: React.FC = () => { const [message, setMessage] = useState<string>(""); useEffect(() => { axios.get("/api/") .then(response => setMessage(response.data.message)) .catch(error => console.error("Error fetching data", error)); }, []); return ( <Container> <Typography variant="h3" gutterBottom> FastAPI + React + Vite + MUI (TypeScript) </Typography> <Typography variant="body1"> {message || "Loading..."} </Typography> <TextField label="Enter something" variant="outlined" /> </Container> ); }; export default App;
This creates a simple input field using Material-UI.
6. Running and Testing the App
Now that everything is set up, let's run both the back-end and front-end to see the full-stack app in action.
Run the Back-End:
Make sure the FastAPI server is running:
Run the Front-End:
In a separate terminal window, run the Vite development server:
This will launch the React app at http://localhost:3000
, and API requests to /api/
will be proxied to the FastAPI back-end.
Wrapping Up
You’ve now set up a full-stack application using FastAPI for the back-end, React (with TypeScript) and Vite for the front-end, and Material-UI for building a modern user interface. You can extend this with more complex features like authentication, database integration, or route protection.
For a more complete, production-ready boilerplate that covers advanced features and follows best practices, check out this full boilerplate. It's an ideal way to accelerate your startup projects!