Effect CommunityEC
Effect Community•8mo ago
amg

Accessing Environment Variables in an Effect Function

👋 I need to access to an environment variable within an effect

export const verifySignature = (requestContent: string, signature: string) => {
  const signingSecret = Config.string("WEBHOOK_SECRET");
  // const signingSecret = process.env.WEBHOOK_SECRET!
  return Effect.try({
    try: () => {
      // Compute the HMAC hash with sha256 algorithm
      const computedSignature = crypto
        .createHmac("sha256", signingSecret)
        .update(requestContent)
        .digest("hex");

      // Compare the provided signature with the computed signature
      const verified = crypto.timingSafeEqual(
        Buffer.from(signature),
        Buffer.from(computedSignature)
      );

      if (!verified) {
        throw new Error("signature no verificada");
      }
      return true;
    },
    catch: () => new HttpUnknownOriginError(),
  });
};


how can I do that? should I pass it as a paremeter? and yield* it from my upper function? or is there another way?
Was this page helpful?