4.7.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.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:

src/api/hello-world/route.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.
Was this chapter helpful?
Ask Bloom
For assistance in your development, use Claude Code Plugins or Docs MCP server in Cursor, VSCode, etc...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
⇧↵