medusajs / roadmap

3 stars 2 forks source link

API Routes #22

Closed olivermrbl closed 5 months ago

olivermrbl commented 5 months ago

API endpoints in Medusa is a place for developers to add custom logic that HTTP requests can trigger. The endpoints have direct access to Medusa’s core services like the Product and Cart services and custom services added to your project or in installed plugins.

API Routes will drastically simplify creating custom API endpoints.

Developers will be able to create custom routes using simple JavaScript without worrying about the intricacies of the Express Router. Routes are registered using the file system, an approach familiar to many from frameworks like Next.js and Svelte.

Here's a basic example of the API:

// api/admin/products/route.ts
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"

export async function GET(req: MedusaRequest, res: MedusaResponse) {
    const productService = req.scope.resolve("productService")

    const products = await productService.list()

    res.json({ products })
}

The example above illustrates a few key points of API Routes:

In the example above, we register a GET route at path /admin/products.

olivermrbl commented 5 months ago

Released in v1.17.2.

Read the announcement post and documentation.