Try it out: https://from.furret.dev/to/replit
This template makes it easy to build and run Cloudflare Workers, which can be published to the Cloudflare Global Network, or run directly from Replit using wrangler dev
(no Cloudflare account required.) In the future, wrangler dev
will be powered by Workerd, our Open-Source Workers runtime.
Click Run in your Replit workspace to set up a Workers project with customizable settings. By default, it will initialize a git repository, set up a TypeScript project, and create a Fetch handler.
This is only needed for publishing the Worker to Cloudflare - it is not needed for running dev mode in Replit.
- Log in to the Cloudflare Dashboard.
- In Account Home, select My Profile on the top right.
- Select API Tokens on the left-hand navigation.
- Select Create Token.
- Go to Edit Cloudflare Workers under API Token Templates and select Use Template.
- Under Account Resources, select All accounts (or a specific account).
- Under Zone Resources, change Specific zone to All zones.
- Select Continue to summary.
- Select Create Token.
- This gives you the API token needed. Copy it to Replit secrets with the name
CLOUDFLARE_API_TOKEN
.
- Log in to the Cloudflare dashboard > Workers.
- In Workers, find your Account ID on the right side of the screen.
- Select Click to copy to copy your Account ID.
- Add this to Replit secrets with the name
CLOUDFLARE_ACCOUNT_ID
.
CLOUDFLARE_ACCOUNT_ID
CLOUDFLARE_API_TOKEN
WRANGLER_SEND_METRICS
By default, Wrangler will prompt for sending metrics every time a Repl is booted up. To silence this prompt:
- Add a Replit secret with the name
WRANGLER_SEND_METRICS
- Use value
true
to send anonymous metrics to Cloudflare, orfalse
to opt-out.
name = "YOUR-WORKER-NAME"
Before deploying your Worker to Cloudflare, make sure that you have completed the Add Cloudflare credentials to Replit secrets step. If you do not add Cloudflare credentials, your project will deploy to a Replit subdomain. After adding Cloudflare credentials to Replit secrets, publish your Worker to the Cloudflare global network. You can publish to a custom domain, or, if not configured, the Worker will publish to a *.workers.dev
subdomain by default. To set up a *.workers.dev
subdomain, go to the Cloudflare dashboard > Workers > Your subdomain > Change. To set up a custom domain, go to Workers in the Cloudflare dashboard > select your Worker > Triggers > Add Custom Domain. To publish your project, run:
npm run deploy
By default, the Run button in your Replit workspace will run your Worker in dev mode, which allows Replit to run your Worker. In the future, this will be backed by our Open-source Runtime, Workerd.
When you deploy your project using the Replit workspace without configuring Cloudflare credentials as secrets, your project will be available on a Replit subdomain as long as the Replit workspace is open. To keep your Worker running when the Replit workspace is closed, enable Always On in your Replit workspace settings.
If you chose to set the Run button to publish to Cloudflare, you can still run the Worker in local development mode with:
npm run start
To persist development data between sessions, run:
npm run start-persist
Tests are a great way to ensure your project works as expected. To run tests at any time, run:
npm run test
By default, replit-run-command
is set to npm run start
, but it can be set to any of the above commands. To change the Run command:
- Open
package.json
. - Update
replit-run-command
to any of the above commands (or any custom command).
Join the Cloudflare Developers community Discord: https://discord.gg/cloudflaredev