5.2.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/hello-world/route.ts with the following content:

src/api/hello-world/route.ts
1import type {2  MedusaRequest,3  MedusaResponse,4} from "@medusajs/framework/http"5
6export const GET = async (7  req: MedusaRequest,8  res: MedusaResponse9) => {10  res.json({11    message: "[GET] Hello world!",12  })13}14
15export const POST = async (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/hello-world.
  • A POST route at http://localhost:9000/hello-world.
Was this chapter helpful?
Edit this page