Skip to main content

How do I access user data in an API endpoint handler?

Objective: This article explains how to acess user data (e.g user id, organisation etc) in an API endpoint handler.

Steps:

  1. Use the context.user object an access user data stored in the JWT.

For more details, see AuthInfo in components/common/types/auth-info.ts

Example:

import type { ApiHandler } from '@hectare/platform.components.context'
import { Response } from '@hectare/platform.components.common'

export const exampleHandler: ApiHandler = async (context): Promise<Response> => {
const userId = context.user.id

return context.event.response.ok(userId)
}