Automation Guide

Cloudflare Workers for Beginners: How to Build Your First API Automation

From an empty folder to a deployed automation that calls one API and sends the result to Slack, in five steps.

Cloudflare WorkersAPIBeginnerJavaScript

By Troy Tessalone · · 10 minutes

Beginner Tutorial

A practical field guide from Automation Ace.

The short answer

To build your first API automation with Cloudflare Workers: create a free Cloudflare account, scaffold a project with npm create cloudflare@latest, write a fetch handler that calls one API and sends the result to another, store any keys as secrets, test with npx wrangler dev, and deploy with npx wrangler deploy. The example below fetches an exchange rate and posts it to Slack in about 30 lines of JavaScript.

For the bigger picture, read Cloudflare Workers 101 first.

What you need

Step 1: create the project

npm create cloudflare@latest my-first-automation
cd my-first-automation

Choose the “Hello World” Worker template and JavaScript (or TypeScript). The wizard can log you in to Cloudflare and deploy for you.

Step 2: write the automation

Replace src/index.js with:

// src/index.js — fetch today's USD→EUR rate and post it to Slack
export default {
  async fetch(request, env, ctx) {
    const result = await runAutomation(env);
    return Response.json(result);
  },
};

async function runAutomation(env) {
  // 1) Call a public API
  const res = await fetch('https://api.frankfurter.app/latest?from=USD&to=EUR');
  if (!res.ok) throw new Error(`Rates API failed: ${res.status}`);
  const data = await res.json();
  const rate = data.rates.EUR;

  // 2) Send the result to another app (Slack incoming webhook)
  const slack = await fetch(env.SLACK_WEBHOOK_URL, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ text: `USD→EUR today: ${rate}` }),
  });
  if (!slack.ok) throw new Error(`Slack failed: ${slack.status}`);

  return { ok: true, rate, date: data.date };
}

This is the core pattern of every API automation: call, check, transform, send. Each fetch() is an HTTP request, and each response is checked before moving on. The JSON handling mirrors what GET and POST requests do in Zapier Code steps.

Step 3: store secrets safely

npx wrangler secret put SLACK_WEBHOOK_URL
# paste the Slack incoming webhook URL when prompted

Secrets are encrypted and exposed to your code as env.SLACK_WEBHOOK_URL. Never hardcode keys in source files. For local testing, put them in a .dev.vars file that is excluded from Git. See API authentication for automation.

Step 4: test locally

npx wrangler dev

Open the local URL Wrangler prints. You should see JSON with the rate, and a message in Slack. If something fails, the error appears in the terminal. See debugging HTTP requests.

Step 5: deploy

npx wrangler deploy

Wrangler prints a *.workers.dev URL. Visiting it runs the automation. Before sharing it, protect the endpoint so strangers cannot trigger it:

// Protect the endpoint with a shared token
const token = request.headers.get('Authorization');
if (token !== `Bearer ${env.RUN_TOKEN}`) {
  return new Response('Unauthorized', { status: 401 });
}

Next steps

Common beginner errors

  • env.X is undefined: the secret was not set for that environment, or .dev.vars is missing locally.
  • 401 or 403 from an API: wrong or missing key. See 401 errors.
  • 429 Too Many Requests: you hit a rate limit. See 429 errors.
  • Unexpected token in JSON: the API returned HTML or an error page; log await res.text() to see it.
  • Exceeded CPU time: heavy processing on the free plan; simplify or move to a paid plan.

Frequently asked questions

How do I create my first Cloudflare Worker?

Install Node.js, run npm create cloudflare@latest, choose the Hello World template, edit src/index.js, test with npx wrangler dev, and deploy with npx wrangler deploy.

How do I call an API from a Cloudflare Worker?

Use the built-in fetch() function with the API URL, method, headers, and body, check response.ok, and parse the response with await response.json().

Where do I store API keys in Cloudflare Workers?

Store them as secrets with npx wrangler secret put NAME and read them from env.NAME in your code. For local development, use a .dev.vars file excluded from Git.

How do I stop strangers from triggering my Worker?

Require a secret token in a header or query string, return 401 when it is missing or wrong, and store the expected token as a Worker secret.

Do I need to know JavaScript to use Cloudflare Workers?

Basic JavaScript helps. Many simple automations follow the same call, check, transform, send pattern, and AI coding assistants can help write and explain the code.

Cloudflare WorkersAPIBeginnerJavaScript

Disclaimer: Zapier features, plan availability, and settings can change. Confirm current details in Zapier's help documentation and Cloudflare's developer documentation and pricing pages. Limits and plan features change over time. Code samples are simplified starting points; add your own validation, error handling, and security review before production use before relying on a specific setting. This article may include links to apps, products, or services; some links may be affiliate links, which means Automation Ace may earn a commission at no extra cost to you.

Build Better Systems

Ready to automate with confidence?

Share your tools, process, and goals. Automation Ace can design the workflow, integration, file transfer, or integration that fits your business.

Start a Project →