Examples of the API Key Module
In this guide, you’ll find common examples of how you can use the API Key Module in your application.
Create an API Key
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IApiKeyModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const apiKeyModuleService: IApiKeyModuleService =
request.scope.resolve(ModuleRegistrationName.API_KEY)
const apiKey = await apiKeyModuleService.create({
title: "Publishable API key",
type: "publishable",
created_by: "user_123",
})
res.json({
api_key: apiKey,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeApiKeyModule,
} from "@medusajs/api-key"
export async function POST(request: Request) {
const apiKeyModuleService = await initializeApiKeyModule()
const apiKey = await apiKeyModuleService.create({
title: "Publishable API key",
type: "publishable",
created_by: "user_123",
})
return NextResponse.json({
api_key: apiKey,
})
}
List API Keys
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IApiKeyModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
request: MedusaRequest,
res: MedusaResponse
) {
const apiKeyModuleService: IApiKeyModuleService =
request.scope.resolve(ModuleRegistrationName.API_KEY)
res.json({
api_keys: await apiKeyModuleService.list(),
})
}
Revoke an API Key
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IApiKeyModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const apiKeyModuleService: IApiKeyModuleService =
request.scope.resolve(ModuleRegistrationName.API_KEY)
const revokedKey = await apiKeyModuleService.revoke(
request.params.id
)
res.json({
api_key: revokedKey,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeApiKeyModule,
} from "@medusajs/api-key"
type ContextType = {
params: {
id: string
}
}
export async function POST(
request: Request,
{ params }: ContextType
) {
const apiKeyModuleService = await initializeApiKeyModule()
const revokedKey = await apiKeyModuleService.revoke(params.id)
return NextResponse.json({
api_key: revokedKey,
})
}
Verify Token
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IApiKeyModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const apiKeyModuleService: IApiKeyModuleService =
request.scope.resolve(ModuleRegistrationName.API_KEY)
const authenticatedToken =
await apiKeyModuleService.authenticate(request.params.id)
res.json({
is_authenticated: !!authenticatedToken,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeApiKeyModule,
} from "@medusajs/api-key"
type ContextType = {
params: {
id: string
}
}
export async function POST(
request: Request,
{ params }: ContextType
) {
const apiKeyModuleService = await initializeApiKeyModule()
const authenticatedToken =
await apiKeyModuleService.authenticate(request.params.id)
return NextResponse.json({
is_authenticated: !!authenticatedToken,
})
}
Roll API Key
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IApiKeyModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
request: MedusaRequest,
res: MedusaResponse
) {
const apiKeyModuleService: IApiKeyModuleService =
request.scope.resolve(ModuleRegistrationName.API_KEY)
const revokedKey = await apiKeyModuleService.revoke(
request.params.id
)
const newKey = await apiKeyModuleService.create({
title: revokedKey.title,
type: revokedKey.type,
created_by: revokedKey.created_by,
})
res.json({
api_key: newKey,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeApiKeyModule,
} from "@medusajs/api-key"
type ContextType = {
params: {
id: string
}
}
export async function POST(
request: Request,
{ params }: ContextType
) {
const apiKeyModuleService = await initializeApiKeyModule()
const revokedKey = await apiKeyModuleService.revoke(params.id)
const newKey = await apiKeyModuleService.create({
title: revokedKey.title,
type: revokedKey.type,
created_by: revokedKey.created_by,
})
return NextResponse.json({
api_key: newKey,
})
}
More Examples
The module interface reference provides a reference to all the methods available for use with examples for each.
Was this section helpful?