Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Jason-AML/MonzaSport-Nextjs/llms.txt

Use this file to discover all available pages before exploring further.

The POST /api/chat route powers the Monza Motors AI assistant. On each request it retrieves the full vehicle catalog via getCollections() and the user’s last 20 chat messages from Supabase, then sends that context alongside the new message to Groq’s llama-3.3-70b-versatile model. Once a response is generated, the AI reply is inserted into the messages table with role: 'assistant' — making it immediately visible to the client through its existing Supabase Realtime subscription.

Endpoint

PropertyValue
MethodPOST
Path/api/chat
AuthAuthenticated user required — user_id scopes message history
Content-Typeapplication/json

Request Body

user_id
string
required
The authenticated user’s Supabase UUID. Used to filter and persist message history in the messages table.
content
string
required
The user’s message text to send to the AI assistant.

Response

200 OK
{ "ok": true }
The AI reply is not included in the HTTP response body. Instead, it is delivered to the client via a Supabase Realtime subscription on the messages table. See the note below for details. 500 Internal Server Error
{ "error": "<error message>" }

Example Request

curl -X POST https://your-app.vercel.app/api/chat \
  -H 'Content-Type: application/json' \
  -d '{"user_id": "uuid-here", "content": "What is the top speed of the Ferrari?"}'

Processing Steps

1

Parse request body

Extract user_id and content from the incoming JSON request body.
2

Fetch vehicle catalog

Call getCollections() to retrieve the full list of vehicles from Supabase. The resulting array is serialised into the AI system prompt as catalog context.
3

Load message history

Query the messages table for the most recent 20 rows where user_id matches, ordered by created_at ascending. This forms the conversation history passed to the model.
4

Generate AI response

Call generateText() from the Vercel AI SDK with groq('llama-3.3-70b-versatile') as the model. The call includes a system prompt containing the full vehicle catalog and the conversation history merged with the new user message.
5

Persist AI reply

Insert the generated text into the messages table with role: 'assistant' and the same user_id. If the insert fails, an error is thrown and a 500 response is returned.
6

Return success

Respond with { ok: true }. The client receives the AI message asynchronously through Supabase Realtime — not from this HTTP response.

System Prompt Context

The system prompt instructs the model to answer only based on the vehicle catalog. Each vehicle entry is formatted as a single colon-delimited line containing the following fields:
FieldDescription
nombre_vehiculoVehicle name
modeloModel designation
anioModel year
precioPrice in USD
motorEngine type / displacement
poder_hpHorsepower output
aceleracion_0_1000–100 km/h acceleration time
velocidad_maximaTop speed
torque_nmTorque in Newton-metres
peso_kgKerb weight in kilograms
descriptionFree-text vehicle description
If the user asks about anything outside the catalog, the model is instructed to politely redirect the conversation back to the available vehicles.
The AI response is not returned in the HTTP response body. After inserting the reply into the messages table, the client’s Supabase Realtime subscription fires and delivers the new row automatically. This design decouples the HTTP response time from the model generation time — the client gets an immediate { ok: true } acknowledgement while the reply streams in through the live database subscription.

Build docs developers (and LLMs) love