FastAPI Backend Setup & Integration
Guide
For: MERN/[Link] Developer
Backend file: [Link] (FastAPI)
Frontend: [Link]
Step 1: Install Python & Set Up the Backend
Prerequisites
1. Install Python 3.9+ from [Link]
2. Open your terminal or VS Code terminal
Project Structure
college-recommender-backend/
│
├── [Link] ← FastAPI backend
└── (optional) .env or [Link]
Setup Python Environment
# Step into the folder
cd college-recommender-backend
# (Optional) Create a virtual environment
python -m venv env
# Activate virtual environment
# Windows:
env\Scripts\activate
# Mac/Linux:
source env/bin/activate
# Install required Python packages
pip install fastapi uvicorn groq pydantic
Run the FastAPI Server
uvicorn try:app --reload
● try: the filename [Link]
● app: FastAPI object in the code
● --reload: auto restart on file changes
👉
Now open this:
[Link] ← Interactive API Explorer
How the Backend Works (Summary)
4 API Endpoints:
Endpoint Method Description
/init_session POST Starts a new chat session
/chat POST Sends user message and receives
response
/profile/{session GET Gets the profile data as JSON
_id}
/download_profile GET Downloads the profile as .txt file
/{id}
Example: How to Use the Endpoints in [Link]
1. /init_session – Start a New Session
// pages/api/[Link]
export async function initSession(apiKey: string) {
const res = await fetch("[Link] {
method: "POST",
headers: { "Content-Type": "application/json" },
body: [Link]({ api_key: apiKey, name: "Lauren" }),
});
return await [Link](); // → { session_id, profile_file }
}
2. /chat – Send a Message to Chatbot
// pages/api/[Link]
export async function sendMessage(sessionId: string, message: string) {
const res = await fetch("[Link] {
method: "POST",
headers: { "Content-Type": "application/json" },
body: [Link]({
session_id: sessionId,
message: message,
history: [], // or maintain history in state
}),
});
return await [Link](); // → { response, profile_status, profile_file }
}
3. /profile/:session_id – Get Full JSON Profile
// pages/api/[Link]
export async function getProfile(sessionId: string) {
const res = await fetch(`[Link]
return await [Link](); // → full profile JSON
}
4. /download_profile/:session_id – Download Profile File
// e.g., from a download button in [Link]
<a href={`[Link] download>
Download Profile
</a>
Where to Get the api_key?
● Go to: [Link]
● Generate your API key and paste it during /init_session.
Summary for Frontend Developer
Task What to Use
Start chatbot session initSession(apiKey)
Chat with bot sendMessage(sessionId, message)
Show progress/profile getProfile(sessionId)
Download profile file <a href="..." download> or
[Link]()
Optional: API Proxy Setup in [Link]
To avoid CORS in production:
[Link]
[Link] = {
async rewrites() {
return [
{
source: "/api/chat",
destination: "[Link]
},
{
source: "/api/init_session",
destination: "[Link]
},
// Add others as needed
];
},
};