API Key Module

In this section of the documentation, you will find resources to learn more about the API Key Module and how to use it in your application.

Medusa has API-key related features available out-of-the-box through the API Key Module. A module is a standalone package that provides features for a single domain. Each of Medusa's commerce features are placed in commerce modules, such as this API Key Module.

NoteLearn more about why modules are isolated in this documentation.

API Key Features#


How to Use the API Key Module#

In your Medusa application, you build flows around commerce modules. A flow is built as a Workflow, which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.

You can build custom workflows and steps. You can also re-use Medusa's workflows and steps, which are provided by the @medusajs/medusa/core-flows package.

For example:

src/workflows/create-api-key.ts
1import { 2  createWorkflow, 3  WorkflowResponse,4  createStep,5  StepResponse,6} from "@medusajs/framework/workflows-sdk"7import { Modules } from "@medusajs/framework/utils"8
9const createApiKeyStep = createStep(10  "create-api-key",11  async ({}, { container }) => {12    const apiKeyModuleService = container.resolve(Modules.API_KEY)13
14    const apiKey = await apiKeyModuleService.createApiKeys({15      title: "Publishable API key",16      type: "publishable",17      created_by: "user_123",18    })19
20    return new StepResponse({ apiKey }, apiKey.id)21  },22  async (apiKeyId, { container }) => {23    const apiKeyModuleService = container.resolve(Modules.API_KEY)24
25    await apiKeyModuleService.deleteApiKeys([apiKeyId])26  }27)28
29export const createApiKeyWorkflow = createWorkflow(30  "create-api-key",31  () => {32    const { apiKey } = createApiKeyStep()33
34    return new WorkflowResponse({35      apiKey,36    })37  }38)

You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:

Learn more about workflows in this documentation.


Was this page helpful?
Edit this page