---
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
---
<!-- docsgraph:related -->
## Related pages

> **For AI agents:** Follow these links to understand how this page connects to the rest of the Vercel ecosystem. For the full cross-link map (inbound, outbound, prerequisites, and semantic neighbors), see the .graph.md link below.

- [Getting Started](https://vercel.com/docs/routing-middleware/getting-started?from=related) — Learn how you can use Routing Middleware, code that executes before a request is processed on a site, to provide speed a
- [Redirects](https://vercel.com/docs/routing/redirects?from=related) — Learn how to use redirects on Vercel to instruct Vercel's platform to redirect incoming requests to a new URL.
- [Backend for Frontend](https://nextjs.org/docs/app/guides/backend-for-frontend?from=related) — Learn how to use Next.js as a backend framework
- [Configuration Redirects](https://vercel.com/docs/routing/redirects/configuration-redirects?from=related) — Learn how to define static redirects in your framework configuration or vercel.json with support for wildcards, pattern
- [Authorization Server API](https://vercel.com/docs/sign-in-with-vercel/authorization-server-api?from=related) — Learn how to use the Authorization Server API
- [Connect your API](https://vercel.com/docs/oidc/api?from=related) — Learn how to configure your own API to trust Vercel's OpenID Connect \(OIDC\) Identity Provider \(IdP\)
- [Modifying request headers](https://vercel.com/kb/guide/modify-request-headers?from=related) — Learn how to modify request headers in your Middleware.
- [Rendering content based on device](https://vercel.com/kb/guide/rendering-content-based-on-device?from=related) — Learn how to render different content based on the user agent in your Middleware.
- [Adding a response header](https://vercel.com/kb/guide/add-response-header?from=related) — Learn how to add a response header in your Middleware.
- [Filtering query parameters](https://vercel.com/kb/guide/filter-query-parameters?from=related) — Learn how to filter query parameters in your Middleware.
- [Can I use Vercel as a reverse proxy?](https://vercel.com/kb/guide/vercel-reverse-proxy-rewrites-external?from=related) — Learn how to use rewrites to proxy requests from Vercel to other deployments.

Full cross-link map for this page: [/kb/guide/use-crypto-web-api.graph.md](/kb/guide/use-crypto-web-api.graph.md)
<!-- /docsgraph:related -->


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)