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
If you are using our Cloudflare getting-started project, you can simply run npm run deploy
Otherwise, you should run
npm i @opennextjs/cloudflare
if it doesn’t exist already, add a deploy script in your package.json
"deploy": "opennextjs-cloudflare build && opennextjs-cloudflare deploy"
Then run
You will be asked to create/connect a Cloudflare account if you don’t already have one connected.
note: you will need to use npm to deploy to Cloudflare because it expects a package-lock.json
file*
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) 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)
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 to deploy your application on GitHub commits. You connect your GitHub repository to your Worker by following these steps.
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
.