How to access Bindings inside custom functions of an RPC Worker?

I need to be able to access the env to be able to reference some vars or other CF services from the RPC worker, this worker will be called from another worker through Service Bindings
import { WorkerEntrypoint } from "cloudflare:workers";

export default class MyRPCWorker extends WorkerEntrypoint {
async fetch(req: Request, env: Env) { console.log("env as the second argument is valid in the fetch handler"); }

async customFunction(param1: string) { console.log("I need to access the env of the current RPC worker here when calling this method from another worker using Service Bindings...") }
}
import { WorkerEntrypoint } from "cloudflare:workers";

export default class MyRPCWorker extends WorkerEntrypoint {
async fetch(req: Request, env: Env) { console.log("env as the second argument is valid in the fetch handler"); }

async customFunction(param1: string) { console.log("I need to access the env of the current RPC worker here when calling this method from another worker using Service Bindings...") }
}
I didn't find anything within docs nor other discussions
1 Reply
Hello, I’m Allie!
Like this:
import { WorkerEntrypoint } from "cloudflare:workers";

export default class MyRPCWorker extends WorkerEntrypoint {
async fetch(req: Request { console.log("env is not on the second argument, but rather on `this`"); }

async customFunction(param1: string) { this.env }
}
import { WorkerEntrypoint } from "cloudflare:workers";

export default class MyRPCWorker extends WorkerEntrypoint {
async fetch(req: Request { console.log("env is not on the second argument, but rather on `this`"); }

async customFunction(param1: string) { this.env }
}

Did you find this page helpful?