If you are using Nuxt, you can benefit from an almost zero-config experience with the @unkey/nuxt module.

Install

  bun install @unkey/nuxt

Configuration

@unkey/nuxt just requires your root key. Create an .env file in your project and add the following:

NUXT_UNKEY_TOKEN=<your api key>

This can also be configured at runtime by setting the NUXT_UNKEY_TOKEN environment variable.

From this point onward, @unkey/nuxt will automatically:

  1. verify any API requests with an Authorization: Bearer xxx header.
  2. register a useUnkey() helper that allows access to an automatically configured unkey instance.

Usage

Automatic verification

You can access the automatically-verified unkey context on the server with event.context.unkey in your server routes or useRequestEvent().context.unkey in the Vue part of your app.

For example:

export default defineEventHandler(async (event) => {
  if (!event.context.unkey.valid) {
    throw createError({ statusCode: 403, message: "Invalid API key" })
  }

  // return authorised information
  return {
    // ...
  };
});

Unkey helper

For more about how to use the configured helper provided by useUnkey(), you can see the API docs for the TypeScript client.

For example:

const unkey = useUnkey();

const created = await unkey.keys.create({
  apiId: "api_7oKUUscTZy22jmVf9THxDA",
  prefix: "xyz",
  byteLength: 16,
  ownerId: "chronark",
  meta: {
    hello: "world",
  },
  expires: 1686941966471,
  ratelimit: {
    async: true,
    duration: 1000,
    limit: 10,
    refillRate: 1,
    refillInterval: 1000,
  },
});

console.log(created.key);

Disable telemetry

By default, Unkey collects anonymous telemetry data to help us understand how our SDKs are used.

If you wish to disable this, you can do so by passing a boolean flag to the constructor:

const unkey = useUnkey({ disableTelemetry: true })