> ## Documentation Index
> Fetch the complete documentation index at: https://docs.layercode.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy Next.js to Cloudflare

> Some tips when deploying a Next.js voice agent to Cloudflare

Layercode runs in our cloud, but you will need to deploy your Next.js application to provide your APIs and agent functionality (LLMs and tool calling).

Plus if you are building for web, your Next.js acts as the client.

This guide assumes you already have your Next.js application running locally with Layercode. If not, pleae follow our [getting started guide](/tutorials/getting-started)

If you are using our Cloudflare getting-started project, you can simply run `npm run deploy`

Otherwise, you should run

```bash theme={null}
  npm i @opennextjs/cloudflare
```

if it doesn't exist already, add a deploy script in your `package.json`

```json theme={null}
"deploy": "opennextjs-cloudflare build && opennextjs-cloudflare deploy"
```

Then run

```bash theme={null}
npm run deploy
```

You will be asked to create/connect a Cloudflare account if you don't already have one connected.

<Note>note: you will need to use npm to deploy to Cloudflare because it expects a `package-lock.json` file\*</Note>

You should see an ouput like this:

```
Total Upload: 5867.42 KiB / gzip: 1177.82 KiB
Worker Startup Time: 25 ms
Your Worker has access to the following bindings:
Binding            Resource
env.ASSETS         Assets

Uploaded jolly-queen-84e7 (16.45 sec)
Deployed jolly-queen-84e7 triggers (4.70 sec)
  https://jolly-queen-84e7.jacksbridger.workers.dev
Current Version ID: 047446f6-055e-46b0-b67a-b45cb14fa8e8
```

Take that url (e.g. [https://jolly-queen-84e7.jacksbridger.workers.dev](https://jolly-queen-84e7.jacksbridger.workers.dev)) of your backend and save it into the Layercode agent backend settings under webhook url (append the appropriate path for your API e.g. [https://jolly-queen-84e7.jacksbridger.workers.dev/api/agent](https://jolly-queen-84e7.jacksbridger.workers.dev/api/agent))

Then your application should run. But please reach out if you run into any issues.

## Setting up automated Cloudflare deployments

You can use [Cloudflare Workers Builds](https://developers.cloudflare.com/workers/ci-cd/builds/) to deploy your application on GitHub commits. You connect your GitHub repository to your Worker by following [these steps](https://developers.cloudflare.com/workers/ci-cd/builds/git-integration/).

In the Build settings:

* The "Build command" should be set to `npx opennextjs-cloudflare build`.
* The "Deploy command" should be set to `npx opennextjs-cloudflare deploy`.
* The environment variables you previously set in `.env` **must** be copied and set in the "Build variables and secrets" section. This is so that `npm next build` executed by Workers Builds will have access to the environment variables. It needs that access to inline the NEXT\_PUBLIC\_... variables and access non-NEXT\_PUBLIC\_... variables needed for SSG pages. If you don't do this, you'll find the NEXT\_PUBLIC\_LAYERCODE\_AGENT\_ID env variable is missing and your voice agent won't work.

Note: do not change your `package.json` build command. It should stay as `next build`.
