H
Hono2mo ago
arma

Accessing .dev.vars outside of context

I use CF Workers and Hono to create an API and to read the secret variables in the .dev.vars you have to go through the context (c) but I want to define my client for several globally because i need to use it in multiple routes, did you know how i can do it? .dev.vars:
APPWRITE_ENDPOINT=
APPWRITE_PROJECT_ID=
APPWRITE_API_KEY=
APPWRITE_ENDPOINT=
APPWRITE_PROJECT_ID=
APPWRITE_API_KEY=
index.ts:
import { Hono } from "hono";
import { Users, Account } from 'node-appwrite';

const app = new Hono();

const client = new Client()
.setEndpoint("") // I want to use APPWRITE_ENDPOINT here
.setProject("") // And APPWRITE_PROJECT_ID here
.setKey(""); // And APPWRITE_API_KEY here

app.get("/users", (c) => {
const users = new Users(client);
....
});

app.get("/create", (c) => {
const account = new Account(client);
account.create();
.....
});

export default app;
import { Hono } from "hono";
import { Users, Account } from 'node-appwrite';

const app = new Hono();

const client = new Client()
.setEndpoint("") // I want to use APPWRITE_ENDPOINT here
.setProject("") // And APPWRITE_PROJECT_ID here
.setKey(""); // And APPWRITE_API_KEY here

app.get("/users", (c) => {
const users = new Users(client);
....
});

app.get("/create", (c) => {
const account = new Account(client);
account.create();
.....
});

export default app;
2 Replies
Hurby
Hurby2mo ago
you can't access Cloudflare environment variables outside of the context Instead, you can create a function, put this client into it, and pass the variables as parameters. That way, you can use that function in whatever route you want.
0x146231489231923
I set up a global variable and set the value to that variable
Want results from more Discord servers?
Add your server