How do I use environment variables?
You might want to set environment variables and secrets in Proxyflare. Wrapping Proxyflare in a PagesFunction
provides access to a Context
with environment variables on ctx.env
.
Environment variables can be managed from your Cloudflare Pages dashboard.
functions/_middleware.ts
import proxyflare from "@flaregun-net/proxyflare-for-pages"
const routes: Route[] = []
// Set API_SERVICE_TOKEN in your Pages dashboard.
type Env = { API_SERVICE_TOKEN: string }
export const onRequest: PagesFunction<Env>[] = [
(ctx) =>
proxyflare({
config: {
routes: [
{
from: { pattern: "proxyflare.works/api/*" },
to: { url: "my-api.com" },
headers: {
request: `Bearer ${ctx.env.API_SERVICE_TOKEN}`,
},
},
],
},
})(ctx),
]
Refer to the Cloudflare docs to learn more about environment variables best practices.