0.18.6 • Published 6 months ago

@unkey/nextjs v0.18.6

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

Installation

npm install @unkey/nextjs

Quickstart

Protecting API routes is as simple as wrapping them with the withUnkey handler:

import { NextRequestWithUnkeyContext, withUnkey } from '@unkey/nextjs';

export const POST = withUnkey(async (req) => {

  // Process the request here
  // You have access to the verification response using `req.unkey`
  console.log(req.unkey);

  return new Response('Your API key is valid!');
});

That’s it! Since this is just handling key verification, there’s no need to specify a root key as an environment variable.

If you want to customize how withUnkey processes incoming requests, you can do so as follows:

getKey

By default, withUnkey will look for a bearer token located in the authorization header. If you want to customize this, you can do so by passing a getter in the configuration object:

export const GET = withUnkey(async (req) => {
  // ...
}, {
	getKey: (req) => new URL(req.url).searchParams.get("key"),
});

onError

You can specify custom error handling. By default errors will be logged to the console, and withUnkey will return a NextResponse with status 500.

export const GET = withUnkey(async (req) => {
  // ...
}, {
	onError: async (req, res) => {
      await analytics.trackEvent(`Error ${res.code}: ${res.message}`)
      return new NextResponse("Unkey error", { status: 500 })
    }
});

handleInvalidKey

Specify what to do if Unkey reports that your key is invalid.

export const GET = withUnkey(async (req) => {
  // ...
}, {
	handleInvalidKey: (req, res) => {
      return new Response("Unauthorized", { status: 401 })
    }
});

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:

export const GET = withUnkey(async (req) => {
  // ...
}, { disableTelemetry: true });

Documentation

Read the full documentation

0.18.6

6 months ago

0.18.5

6 months ago

0.18.4

6 months ago

0.18.3

7 months ago

0.18.2

7 months ago

0.18.1

7 months ago

0.18.0

8 months ago

0.17.7

9 months ago

0.17.6

10 months ago

0.17.4

11 months ago

0.17.5

11 months ago

0.17.3

11 months ago

0.17.2

12 months ago

0.17.0

1 year ago

0.17.1

1 year ago

0.16.0

1 year ago

0.15.13

1 year ago

0.15.14

1 year ago

0.15.12

1 year ago

0.15.15

1 year ago

0.15.16

1 year ago

0.15.11

1 year ago

0.15.10

1 year ago

0.15.9

1 year ago

0.15.8

1 year ago

0.15.7

1 year ago

0.15.6

1 year ago

0.15.5

1 year ago

0.15.4

1 year ago

0.15.3

1 year ago

0.15.2

1 year ago

0.15.1

1 year ago

0.15.1-canary.0

1 year ago

0.15.0

1 year ago

0.14.0

1 year ago

0.13.8-canary.1

1 year ago

0.13.8-canary.2

1 year ago

0.13.8-canary.0

1 year ago

0.13.8

1 year ago

0.13.7

1 year ago

0.13.6

2 years ago

0.13.5

2 years ago

0.13.4

2 years ago

0.13.3

2 years ago

0.13.2

2 years ago

0.13.1

2 years ago

0.13.0

2 years ago

0.12.0

2 years ago

0.11.1

2 years ago

0.11.0

2 years ago

0.10.0

2 years ago

0.9.9

2 years ago

0.9.9-canary.0

2 years ago

0.9.6

2 years ago

0.9.4

2 years ago

0.9.3

2 years ago

0.9.2

2 years ago

0.9.1

2 years ago

0.9.0

2 years ago

0.8.0

2 years ago

0.8.0-canary.0

2 years ago

0.7.2

2 years ago

0.7.1

2 years ago

0.7.1-canary.0

2 years ago

0.7.0

2 years ago

0.6.24

2 years ago

0.6.24-canary.0

2 years ago

0.6.23

2 years ago

0.6.22

2 years ago

0.6.22-canary.1

2 years ago

0.6.22-canary.0

2 years ago