Back to Medusa

{metadata.title}

www/apps/book/app/learn/fundamentals/api-routes/http-methods/page.mdx

2.14.21.0 KB
Original Source

export const metadata = { title: ${pageNumber} HTTP Methods, }

{metadata.title}

In this chapter, you'll learn about how to add new API routes for each HTTP method.

HTTP Method Handler

An API route is created for every HTTP method you export a handler function for in a route.ts or route.js file.

Allowed HTTP methods are: GET, POST, DELETE, PUT, PATCH, OPTIONS, and HEAD.

For example, create the file src/api/hello-world/route.ts with the following content:

ts
import type {
  MedusaRequest,
  MedusaResponse,
} from "@medusajs/framework/http"

export const GET = async (
  req: MedusaRequest,
  res: MedusaResponse
) => {
  res.json({
    message: "[GET] Hello world!",
  })
}

export const POST = async (
  req: MedusaRequest,
  res: MedusaResponse
) => {
  res.json({
    message: "[POST] Hello world!",
  })
}

This file adds two API Routes:

  • A GET API route at http://localhost:9000/hello-world.
  • A POST API route at http://localhost:9000/hello-world.