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
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break