www/apps/book/app/learn/fundamentals/api-routes/responses/page.mdx
export const metadata = {
title: ${pageNumber} API Route Response,
}
In this chapter, you'll learn how to send a response in your API route.
To send a JSON response, use the json method of the MedusaResponse object that is passed as the second parameter of your API route handler.
For example:
export const jsonHighlights = [ ["7", "json", "Return a JSON object."] ]
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
export const GET = async (
req: MedusaRequest,
res: MedusaResponse
) => {
res.json({
message: "Hello, World!",
})
}
This API route returns the following JSON object:
{
"message": "Hello, World!"
}
By default, setting the JSON data using the json method returns a response with a 200 status code.
To change the status code, use the status method of the MedusaResponse object.
For example:
export const statusHighlight = [
["7", "status", "Set the response code to 201."]
]
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
export const GET = async (
req: MedusaRequest,
res: MedusaResponse
) => {
res.status(200).json({
message: "Hello, World!",
})
}
The response of this API route has the status code 201.
To return response data other than a JSON object, you can either use the set, setHeader, or writeHead methods of the MedusaResponse object. They allow you to set the response headers, including the content type.
To create an API route that returns a PDF file, you can set the Content-Type header to application/pdf:
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
export const GET = async (
req: MedusaRequest,
res: MedusaResponse
) => {
// assuming you have the PDF file as buffer
res.set({
"Content-Type": "application/pdf",
"Content-Disposition": `attachment; filename="invoice-${id}.pdf"`,
"Content-Length": buffer.length,
})
res.send(buffer)
}
The set method allows you to set multiple headers at once. It accepts an object of key-value header pairs.
To create an API route that returns XML content, you can set the Content-Type header to application/xml:
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
export const GET = async (
req: MedusaRequest,
res: MedusaResponse
) => {
// Assuming you have XML string
res.set({
"Content-Type": "application/xml; charset=utf-8",
})
res.send(xmlString)
}
To create an API route that returns server-sent events (SSE), you can set the Content-Type header to text/event-stream:
export const streamHighlights = [
["7", "writeHead", "Set the response's headers."],
["7", "200", "Set the status code."],
["8", "Content-Type", "Set the response's content type."],
["13", "interval", "Simulate stream data using an interval."],
["14", "write", "Write stream data."],
["17", "on", "Stop the stream when the request is terminated."]
]
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
export const GET = async (
req: MedusaRequest,
res: MedusaResponse
) => {
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
})
const interval = setInterval(() => {
res.write("data: Streaming data...\n\n")
}, 3000)
req.on("close", () => {
clearInterval(interval)
res.end()
})
req.on("end", () => {
clearInterval(interval)
res.end()
})
}
The writeHead method accepts two parameters:
This API route opens a stream by setting the Content-Type to text/event-stream. It then simulates a stream by creating an interval that writes the stream data every three seconds.
The JS SDK has a fetchStream method that you can use to fetch data from an API route that returns a stream.
Learn more in the JS SDK documentation.
The MedusaResponse type is based on Express's Response. Refer to their API reference for other uses of responses.