Simple Next.js boilerplate that uses BOTH FastAPI AND NEXTJS 13 as the API backend unlike the example offered by NextJS
This is a hybrid Next.js + Python app that uses a fullstack Next.js application and FastAPI as another API backend. One great use case of this is to write Next.js apps that use Python AI libraries on the backend.
The Python/FastAPI server is mapped into to Next.js app under /api/py/
(easily changeable) and the NextJS is mapped to /api/
.
This is implemented using next.config.js
rewrites to map any request to /api/py/:path*
to the FastAPI API, which is hosted in the /api
folder.
On localhost, the rewrite will be made to the 127.0.0.1:8000
port, which is where the FastAPI server is running.
In production, the FastAPI server is hosted as Python serverless functions on Vercel.
https://nextjs-fastapi-starter.vercel.app/
You can clone & deploy it to Vercel with one click:
You can clone & create this repo with the following command
npx create-next-app nextjs-fastapi --example "https://github.com/psycho-baller/nextjs-and-fastapi-backend"
First, install the dependencies:
npm install
# or
yarn
# or
pnpm install
Create a virtual environment and install the dependencies for the FastAPI server:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Then, run the development server:
npm run dev
# or
yarn dev
# or
pnpm dev
Open http://localhost:3000 with your browser to see the result.
The FastApi server will be running on http://127.0.0.1:8000 – feel free to change the port in package.json
(you'll also need to update it in next.config.js
).
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
- FastAPI Documentation - learn about FastAPI features and API.
You can check out the Next.js GitHub repository - your feedback and contributions are welcome!