Skip to content

ApiRegistration

Accessed via ev.addonApi. Only operable inside the router.beforeEvents.startup event.

typescript
interface ApiRegistration {
  register<TArgs, TReturn>(apiName: string, handler: (args: TArgs, ctx: ApiHandlerContext) => TReturn | Promise<TReturn>): void
  hook<TArgs, TReturn>(targetAddonId: string, apiName: string, options: HookOptions<TArgs, TReturn>): void
}

Methods

hook

typescript
hook<TArgs, TReturn>(
  targetAddonId: string,
  apiName: string,
  options: HookOptions<TArgs, TReturn>,
): void

Registers a hook that intercepts another addon's API calls.

Parameters

  • targetAddonId: string

    The ID of the addon to hook into.

  • apiName: string

    The name of the API to hook.

  • options: HookOptions<TArgs, TReturn>

    Hook configuration.

Returns: void

typescript
// Mutate args
ev.addonApi.hook('economy-addon', 'economy/getBalance', {
  before: async (ctx) => {
    ctx.args = { ...ctx.args, audited: true }
  },
})

// Post-process result
ev.addonApi.hook('economy-addon', 'economy/getBalance', {
  after: async (ctx) => {
    ctx.result = { ...ctx.result, taxRate: 0.1 }
  },
})

register

typescript
register<TArgs, TReturn>(
  apiName: string,
  handler: (args: TArgs, ctx: ApiHandlerContext) => TReturn | Promise<TReturn>,
): void

Registers an API handler provided by this addon. Registering the same apiName twice within the same addonId throws an error.

Parameters

  • apiName: string

    The name of the API to register.

  • handler: (args: TArgs, ctx: ApiHandlerContext) => TReturn | Promise<TReturn>

    The handler to invoke when the API is called. ctx.callerAddonId identifies the caller.

Returns: void

typescript
ev.addonApi.register<{ playerId: string }, { balance: number }>(
  'economy/getBalance',
  async ({ playerId }, ctx) => {
    console.log(`Called by ${ctx.callerAddonId}`)
    return { balance: getBalance(playerId) }
  },
)

API Name Namespacing

Use slash-separated namespaces like economy/getBalance to avoid collisions and improve discoverability.

Released under the MIT License.