Table of contents
This guide will show you how to build a free hosted Cloudflare Worker that:
You might know of Cloudflare as a DNS provider, or as a CDN, or as a DDoS protection service. But Cloudflare is also a platform for building modern web applications. We run a lot of Replicate's own infrastructure on Cloudflare, and we're big fans.
Cloudflare has dozens of products for building web applications, but in this guide we'll be using just a few of them:
You know those websites that generate placeholder images for web design prototypes? You give it a width and height and it generates a placeholder image of that size.
We're going to build one of those, but with a bit more pizzaz:
We'll call it "Placeholder Zoo".
To build this app, we'll create a Cloudflare Worker that does the following:
https://example.com/800x600/sunglasses-sloth
.Here's what you'll need to build this project:
The Cloudflare team maintains an official CLI tool called create-cloudflare (also known as C3) that helps you set up and deploy new applications to Cloudflare. It's an npm package that you can run directly from the command line.
Run the following command to get started:
This commands takes care of a lot of things for you:
.gitignore
file to avoid committing secrets to your repository.This will also automatically open a browser window to your new worker's URL:
Your new worker is now deployed to Cloudflare, but you can also run it locally.
Run the worker on your local machine to make sure everything is set up correctly:
You should see output like this:
Hit b to open the worker in your browser. You should see the "Hello, world!" message.
Now that you've got your worker running locally, it's time to add some secrets so your app can make authenticated requests to Replicate and Cloudflare.
Start by creating a file called .dev.vars
. Cloudflare uses this file to store secrets locally for your worker.
Then add the following placeholder values to the .dev.vars
file:
The values that go in this file are secrets, so you should treat them like passwords. Luckily, the npm create cloudflare
command you ran created a .gitignore
file that ignores the .dev.vars
file, so you don't have to worry about accidentally committing it to your repository.
Replicate API token
You'll need a Replicate API token so you can start running models from your worker.
Go to replicate.com/account/api-tokens and create a new API token, then copy it to your clipboard.
Then paste your Replicate API token into the .dev.vars
file for local development:
You'll also need to set the REPLICATE_API_TOKEN
secret in your remote worker's configuration.
Start by logging into Cloudflare using the wrangler CLI:
Note: You'll use npx wrangler
to run wrangler everywhere in this guide. Using npx ensures you're using the version of wrangler that is installed locally in the project, rather than a globally isntalled npm package, which can vary from one machine to another.
Then set your Replicate token as a secret on your deployed worker:
You should see output like this:
Cloudflare account ID
To find your Cloudflare account ID, run this command in the terminal:
Set the CLOUDFLARE_ACCOUNT_ID
secret in your .dev.vars
file for local development:
You'll also need to set the CLOUDFLARE_ACCOUNT_ID
secret in your remote worker's configuration:
Cloudflare API token
To create a Cloudflare API token for Cloudflare Images, do the following:
Set the CLOUDFLARE_API_TOKEN
secret in your .dev.vars
file for local development:
You'll also need to set the CLOUDFLARE_API_TOKEN
secret in your remote worker's configuration:
Cloudflare Images account hash
You'll need your Cloudflare Images account hash so you can construct URLs to your generated images.
To find your Cloudflare Images account hash:
Set the CLOUDFLARE_IMAGE_ACCOUNT_HASH
secret in your .dev.vars
file for local development:
You'll also need to set the CLOUDFLARE_IMAGE_ACCOUNT_HASH
secret in your remote worker's configuration:
Now that you've added all the secrets, run this command to check that you've set them up correctly on your deployed worker:
You should see output like this:
Now that your worker is running locally and your secrets are set up, you can start running models on Replicate from your worker code.
Install the replicate
npm package:
Create a new file called src/image-generator.ts
:
Paste the following code into the file:
Then create a new file called src/homepage.ts
:
Then paste the following code into the src/homepage.ts
file:
Replace the contents of your src/index.ts
file with the following code:
Now run the worker again:
Open this URL in your browser: localhost:8787/1600x900/cinephile-rat
You should see a generated image that looks something like this:
This is another good time to commit your changes to Git:
When you run models with Replicate's API, any output files generated by the model are returned as HTTPS URLs that are automatically deleted after an hour. If you want to keep your output files for longer than an hour, you need to save a copy of them somewhere.
You'll use Cloudflare Images to store your generated images, and Cloudflare KV as a key-value datastore so you can do quick lookups of images that have already been generated.
Create a new file called src/image-uploader.ts
:
Then add the following code to the file:
Then add this line to your src/index.ts
file, after the generateImage
function:
Each time you upload an image to Cloudflare Images, you'll also store some metadata about the image in Cloudflare KV. This will let you quickly look up whether an image has already been generated, so you can return the existing image without re-running the model.
Use Wrangler to create a new KV namespace:
You should see output like this:
The "configuration file" the above message is referring to is your wrangler.toml
file.
Next, update your worker code to check the KV store before generating an image, and cache the generated image ID in KV if it's not already cached.
You'll store the request pathname (e.g. /800x600/sunglasses-sloth
) as a key, and the Cloudflare Images ID (e.g. ab6b3a38-1957-4b8d-91de-3aadf0f22211
) as the value.
Overwrite the contents of your src/index.ts
file with the following code:
You've been iterating on your worker locally, so now it's time to deploy your changes to Cloudflare:
You should see output like this:
Go to the URL in the output and you should see a landing page with links to example images.
If you encounter any errors when running your remote worker, you can use the wrangler tail
command to see the logs:
Congratulations! You've built a Cloudflare Worker that:
Here are some suggestions for next steps:
Happy hacking!