Cloudflare Workers environment variables and context
Cloudflare Workers does not set environment variables a global object like Node.js does with process.env
. Workers binds environment variables to the worker's special fetch
event handler thought a specific env
argument.
This means accessing environment variables within Inngest function handlers isn't possible without explicitly passing them through from the worker event handler to the Inngest function handler.
We can accomplish this by use the middleware feature for Workers or when using Hono.
Creating middleware
You can create middleware which extracts the env
argument from the Workers fetch
event handler arguments for either Workers or Hono:
- Use
onFunctionRun
'sreqArgs
array to get theenv
object and, optionally, cast a type. - Return the
env
object within the specialctx
object oftransformInput
lifecycle method.
import { Inngest, InngestMiddleware } from 'inngest';
const bindings = new InngestMiddleware({
name: 'Cloudflare Workers bindings',
init({ client, fn }) {
return {
onFunctionRun({ ctx, fn, steps, reqArgs }) {
return {
transformInput({ ctx, fn, steps }) {
// reqArgs is the array of arguments passed to the Worker's fetch event handler
// ex. fetch(request, env, ctx)
// We cast the argument to the global Env var that Wrangler generates:
const env = reqArgs[1] as Env;
return {
ctx: {
// Return the env object to the function handler's input args
env,
},
};
},
};
},
};
},
});
// Include the middleware when creating the Inngest client
export const inngest = new Inngest({
id: 'my-workers-app',
middleware: [bindings],
});
Within your functions, you can now access the environment variables via the env
object argument that you returned in transformInput
above. Here's an example function:
const myFn = inngest.createFunction(
{ id: 'my-fn' },
{ event: 'demo/event.sent' },
// The "env" argument returned in transformInput is passed through:
async ({ event, step, env }) => {
// The env object will be typed as well:
console.log(env.MY_VAR);
}
);