---
title: Using the crypto Web API to redirect requests with a unique token
description: Learn how to use the Crypto Web API in your Middleware.
url: /kb/guide/use-crypto-web-api
canonical_url: "https://vercel.com/kb/guide/use-crypto-web-api"
published: 2025-11-03
last_updated: 2025-11-11
authors: DX Team
related:
  - /docs/routing-middleware
  - /docs/routing-middleware/api
install_vercel_plugin: npx plugins add vercel/vercel-plugin
---

The following example shows how to use the `crypto` Web API in your Middleware to redirect requests with a unique token.

Your `middleware` file should be placed at the root of your project. If you are using the `src` directory, the file should be placed in the `src` directory.

```js
import { NextResponse } from 'next/server';

export const config = {
  matcher: '/',
};

export default function middleware(request) {
  const token = crypto.randomUUID();
  const url = request.nextUrl;

  url.pathname = '/api/crypto';
  url.searchParams.set('token', token);

  return NextResponse.redirect(url);
}
```
```ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

export const config = {
  matcher: '/',
};

export default function middleware(request: NextRequest) {
  const token = crypto.randomUUID();
  const url = request.nextUrl;

  url.pathname = '/api/crypto';
  url.searchParams.set('token', token);

  return NextResponse.redirect(url);
}
```

## More resources

- [Middleware](/docs/routing-middleware)
  
- [Middleware API](/docs/routing-middleware/api)