4.1.1. HTTP Methods

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 file.

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

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

src/api/store/hello-world/route.ts
1import type {2  MedusaRequest,3  MedusaResponse,4} from "@medusajs/medusa"5
6export const GET = (7  req: MedusaRequest,8  res: MedusaResponse9) => {10  res.json({11    message: "[GET] Hello world!",12  })13}14
15export const POST = (16  req: MedusaRequest,17  res: MedusaResponse18) => {19  res.json({20    message: "[POST] Hello world!",21  })22}

This adds two API Routes:

  • A GET route at http://localhost:9000/store/hello-world.
  • A POST route at http://localhost:9000/store/hello-world.
Was this chapter helpful?