Repository: digitros/nextjs-fastapi Branch: main Commit: 081a81f54804 Files: 15 Total size: 14.6 KB Directory structure: gitextract_9mwtj9ca/ ├── .eslintrc.json ├── .gitignore ├── LICENSE ├── README.md ├── api/ │ └── index.py ├── app/ │ ├── api/ │ │ └── helloNextJs/ │ │ └── route.ts │ ├── globals.css │ ├── layout.tsx │ └── page.tsx ├── next.config.js ├── package.json ├── postcss.config.js ├── requirements.txt ├── tailwind.config.js └── tsconfig.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .eslintrc.json ================================================ { "extends": "next/core-web-vitals" } ================================================ FILE: .gitignore ================================================ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # dependencies /node_modules /.pnp .pnp.js # testing /coverage # next.js /.next/ /out/ # production /build # misc .DS_Store *.pem # debug npm-debug.log* yarn-debug.log* yarn-error.log* # local env files .env*.local # vercel .vercel # typescript *.tsbuildinfo next-env.d.ts # python __pycache__/ venv ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2023 Diego Valdez Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================

Next.js FastAPI Starter

Simple Next.j 14 boilerplate that uses FastAPI as the API backend.


## Introduction This is a hybrid Next.js 14 + Python template. One great use case of this is to write Next.js apps that use Python AI libraries on the backend, while still having the benefits of Next.js Route Handlers and Server Side Rendering. ## How It Works The Python/FastAPI server is mapped into to Next.js app under `/api/`. This is implemented using [`next.config.js` rewrites](https://github.com/digitros/nextjs-fastapi/blob/main/next.config.js) to map any request to `/api/py/:path*` to the FastAPI API, which is hosted in the `/api` folder. Also, the app/api routes are available on the same domain, so you can use NextJs Route Handlers and make requests to `/api/...`. 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](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python) on Vercel. ## Demo https://nextjs-fastapi-starter.vercel.app/ ## Deploy Your Own You can clone & deploy it to Vercel with one click: [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fdigitros%2Fnextjs-fastapi%2Ftree%2Fmain) ## Developing Locally You can clone & create this repo with the following command ```bash npx create-next-app nextjs-fastapi --example "https://github.com/digitros/nextjs-fastapi" ``` ## Getting Started First, create and activate a virtual environment: ```bash python3 -m venv venv source venv/bin/activate ``` Then, install the dependencies: ```bash npm install # or yarn # or pnpm install ``` Then, run the development server(python dependencies will be installed automatically here): ```bash npm run dev # or yarn dev # or pnpm dev ``` Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. The FastApi server will be running on [http://127.0.0.1:8000](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`). ## Learn More To learn more about Next.js, take a look at the following resources: - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. - [FastAPI Documentation](https://fastapi.tiangolo.com/) - learn about FastAPI features and API. You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! ================================================ FILE: api/index.py ================================================ from fastapi import FastAPI ### Create FastAPI instance with custom docs and openapi url app = FastAPI(docs_url="/api/py/docs", openapi_url="/api/py/openapi.json") @app.get("/api/py/helloFastApi") def hello_fast_api(): return {"message": "Hello from FastAPI"} ================================================ FILE: app/api/helloNextJs/route.ts ================================================ import { NextResponse } from "next/server"; export async function GET(request: Request) { return NextResponse.json({ message: "Hello from Next.js 14" }); } ================================================ FILE: app/globals.css ================================================ @tailwind base; @tailwind components; @tailwind utilities; :root { --foreground-rgb: 0, 0, 0; --background-start-rgb: 214, 219, 220; --background-end-rgb: 255, 255, 255; } @media (prefers-color-scheme: dark) { :root { --foreground-rgb: 255, 255, 255; --background-start-rgb: 0, 0, 0; --background-end-rgb: 0, 0, 0; } } body { color: rgb(var(--foreground-rgb)); background: linear-gradient( to bottom, transparent, rgb(var(--background-end-rgb)) ) rgb(var(--background-start-rgb)); } ================================================ FILE: app/layout.tsx ================================================ import "./globals.css"; import { Inter } from "next/font/google"; const inter = Inter({ subsets: ["latin"] }); export const metadata = { title: "Create Next App", description: "Generated by create next app", }; export default function RootLayout({ children, }: { children: React.ReactNode; }) { return ( {children} ); } ================================================ FILE: app/page.tsx ================================================ import Image from "next/image"; import Link from "next/link"; export default function Home() { return (

Get started by editing FastApi API  api/index.py

Get started by editing Next.js API  app/api/helloNextJs

By{" "} Vercel Logo
Next.js Logo

Docs{" "} ->

Find in-depth information about Next.js features and API.

Learn{" "} ->

Learn about Next.js in an interactive course with quizzes!

Templates{" "} ->

Explore the Next.js 13 playground.

Deploy{" "} ->

Instantly deploy your Next.js site to a shareable URL with Vercel.

); } ================================================ FILE: next.config.js ================================================ /** @type {import('next').NextConfig} */ const nextConfig = { rewrites: async () => { return [ { source: "/api/py/:path*", destination: process.env.NODE_ENV === "development" ? "http://127.0.0.1:8000/api/py/:path*" : "/api/", }, { source: "/docs", destination: process.env.NODE_ENV === "development" ? "http://127.0.0.1:8000/api/py/docs" : "/api/py/docs", }, { source: "/openapi.json", destination: process.env.NODE_ENV === "development" ? "http://127.0.0.1:8000/api/py/openapi.json" : "/api/py/openapi.json", }, ]; }, }; module.exports = nextConfig; ================================================ FILE: package.json ================================================ { "name": "nextjs-fastapi", "version": "0.2.0", "private": true, "scripts": { "fastapi-dev": "pip3 install -r requirements.txt && python3 -m uvicorn api.index:app --reload", "next-dev": "next dev", "dev": "concurrently \"npm run next-dev\" \"npm run fastapi-dev\"", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "@types/node": "22.5.5", "@types/react": "18.3.8", "@types/react-dom": "18.3.0", "autoprefixer": "10.4.20", "concurrently": "^9.0.1", "eslint": "8.41.0", "eslint-config-next": "13.4.4", "next": "^14.2.13", "postcss": "^8.4.47", "react": "18.3.1", "react-dom": "18.3.1", "tailwindcss": "3.4.12", "typescript": "5.6.2" } } ================================================ FILE: postcss.config.js ================================================ module.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, }, } ================================================ FILE: requirements.txt ================================================ fastapi==0.115.0 uvicorn[standard]==0.30.6 ================================================ FILE: tailwind.config.js ================================================ /** @type {import('tailwindcss').Config} */ module.exports = { content: [ './pages/**/*.{js,ts,jsx,tsx,mdx}', './components/**/*.{js,ts,jsx,tsx,mdx}', './app/**/*.{js,ts,jsx,tsx,mdx}', ], theme: { extend: { backgroundImage: { 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 'gradient-conic': 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', }, }, }, plugins: [], } ================================================ FILE: tsconfig.json ================================================ { "compilerOptions": { "target": "es5", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, "strict": true, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "jsx": "preserve", "incremental": true, "plugins": [ { "name": "next" } ], "paths": { "@/*": ["./*"] } }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], "exclude": ["node_modules"] }