© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Cloudflare DevelopersCD
Cloudflare Developers•15mo ago•
2 replies
Nlea

How to access environment variables in Cloudflare Workers when using RPC binding

I'm working with two separate Cloudflare Workers, where Worker A calls a function in Worker B via a binding (RPC). Worker B requires an environment variable to send an email, and this variable should be scoped to Worker B itself, so it doesn't depend on Worker A's environment.

Here's the relevant code for Worker B:
export class WorkerEmail extends WorkerEntrypoint {
  // Currently, entrypoints without a named handler are not supported

  async fetch(request: Request): Promise<Response> {
    const url = new URL(request.url);
    if (url.pathname === "/send" && request.method === "POST") {
      const { email, firstName }: { email: string; firstName: string } =
        await request.json();
      return this.send(email, firstName, this.env as Env); // Attempting to pass env here
    }
    return new Response(null, { status: 404 });
  }

  
  async send(email: string, firstName: string, env: Env): Promise<Response> {
    const resend = new Resend(env.RESEND_API); // env.RESEND_API should come from Worker B's environment
    
  }
}
export class WorkerEmail extends WorkerEntrypoint {
  // Currently, entrypoints without a named handler are not supported

  async fetch(request: Request): Promise<Response> {
    const url = new URL(request.url);
    if (url.pathname === "/send" && request.method === "POST") {
      const { email, firstName }: { email: string; firstName: string } =
        await request.json();
      return this.send(email, firstName, this.env as Env); // Attempting to pass env here
    }
    return new Response(null, { status: 404 });
  }

  
  async send(email: string, firstName: string, env: Env): Promise<Response> {
    const resend = new Resend(env.RESEND_API); // env.RESEND_API should come from Worker B's environment
    
  }
}


When I use HTTP with fetch in Worker A, it works fine:

await c.env.WORKER_EMAIL.fetch(
  new Request("https://worker-email/send", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ email, firstName }),
  })
);
await c.env.WORKER_EMAIL.fetch(
  new Request("https://worker-email/send", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ email, firstName }),
  })
);


However, if I want to call Worker B directly via an RPC-style binding (like
await c.env.WORKER_EMAIL.send(email, firstName)
await c.env.WORKER_EMAIL.send(email, firstName)
), I run into issues with accessing the environment variable
env.RESEND_API
env.RESEND_API
directly in Worker B. It seems the environment is not properly scoped when using RPC and it stays undefined

Question: How can I pass the correct environment to Worker B's send function when using RPC? Or is there a better way to structure this to ensure Worker B can access its own environment variables independently of Worker A?

What am I missing here? Any guidance would be greatly appreciated!
Cloudflare Developers banner
Cloudflare DevelopersJoin
Welcome to the official Cloudflare Developers server. Here you can ask for help and stay updated with the latest news
85,042Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

How to test two binding workers in dev environment?
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
2y ago
Monorepo on Cloudflare Workers: Unable to pass down Environment Variables
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
4mo ago
How to Access System Environment Variables for Development?
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
2y ago
Cloudflare access JWT validation in cloudflare workers
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3y ago