Skip to content

Quick Start

This guide will walk you through creating a simple Inertia.js app with FastAPI and React.

from fastapi import FastAPI
from inertia.fastapi import InertiaDep
app = FastAPI()
@app.get("/")
async def home(inertia: InertiaDep):
return inertia.render("Home", {
"message": "Hello from Inertia!"
})

Create templates/app.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
{{ vite()|safe }}
</head>
<body>
<div id="app" data-page='{{ page }}'></div>
</body>
</html>

Create frontend/app.tsx:

import { createRoot } from 'react-dom/client'
import { createInertiaApp } from '@inertiajs/react'
createInertiaApp({
resolve: (name) => {
const pages = import.meta.glob('./pages/**/*.tsx', { eager: true })
return pages[`./pages/${name}.tsx`]
},
setup({ el, App, props }) {
createRoot(el).render(<App {...props} />)
},
})

Create frontend/pages/Home.tsx:

export default function Home({ message }: { message: string }) {
return (
<div>
<h1>{message}</h1>
<p>Welcome to Inertia.js with FastAPI!</p>
</div>
)
}

Terminal 1 - Start Vite:

Terminal window
npm run dev
# or
bun run dev

Terminal 2 - Start FastAPI:

Terminal window
uvicorn main:app --reload

Visit http://localhost:8000 and you should see your app!