H
HonoChicha

use Context to get vars from wrangler.toml

how can fix this code. i want to use my openaiApiKey from the wrangler.toml when i create an openai instance. but i cannot use the context . how can i fix it ?
No description
terenced
terenced29d ago
Hey! I’m new to Hono, but think there might be some confusion around context and environment variables. Hono pass a context with each request and it should only contain info related to the request, like headers. In your wrangler.toml, you are setting environment variables, which are available wherever your code it running. Think of it like a global variable set by the environment. Have a look at https://developers.cloudflare.com/workers/configuration/environment-variables/ and https://hono.dev/api/context. I hope this helps (and that I am actually right 😊)
Cloudflare Docs
Environment variables · Cloudflare Workers docs
Attach text strings and JSON values as environment variables to your Worker.
Nico
Nico29d ago
You are right when it comes to working in Cloudflare, however, since we don't have access to that fetch request in Hono we can't access the env variables. That is why they are only available in the context. In your case you would probbaly want to put this in a middleware for all routes If you can give me an example of what you are tyring to do with it we can place it in the most optimal area
terenced
terenced29d ago
ah interesting, I didn't know that! Thank you @Nico 😄
Chicha
Chicha26d ago
@Nico this is how i fix it
Chicha
Chicha26d ago
No description
Nico
Nico26d ago
That looks good, use await next instead of return next in your middleware. Middleware don’t need returns
Chicha
Chicha26d ago
ok thanks
predaytor
predaytor25d ago
for type infer use:
export type ContextEnv = {
Bindings: Required<Env>;
Variables: {
user: User | null;
session: Session | null;
};
};

const app = new Hono<ContextEnv>();
export type ContextEnv = {
Bindings: Required<Env>;
Variables: {
user: User | null;
session: Session | null;
};
};

const app = new Hono<ContextEnv>();
Want results from more Discord servers?
Add your server
More Posts
Connect Hono Cloudflare with MongoDBHi. I am using Hono and deploying to Cloudflare. However I am unable to find a way to connect mongodCase insensitive query paramsWe are using hono-zod-openapi to replace an existing API. Sadly the old API (written in ASP.Net) appPassword hashingHow to hash password?How to setup Hono with Remix/Cloudflare for Vite dev server?From the documentation for Cloudflare Pages, there is an adapter and a plugin, but it is not clear hValidate response objectsUsing the zod validator middleware it is possible to validate params , query, request body like: ``Hono Oauth Provider with JWT issueHey everyone! I'm not sure if this is the right place for me to post about this but recently I wanteDeploy Hono / Bun APII know, I know this is such a newby question but I come from small express apps and other simple ‘clIs there a way to list all request headers ?I have this use case where I am not sure which header key is being sent, I would like to log all heaTyping Middleware JWT and othersCurrently I am trying to type the `c.get('jwtPayload')` that is documented [here](https://hono.dev/mHow to generate openapi.yaml spec with Zod OpenAPI Hono?Is it possible to generate the openapi.yaml spec with Zod OpenAPI Hono from the commandline using a Why use Hono for Next.js backend (route handlers)?I'm creating a full-stack web app and probably using Next.js API/route handler: https://nextjs.org/dContext not finalizedIm trying to build a S3 API and it returns this on every requests ```js Error: Context is not finalType checking with Hono + prisma + zod -> return json date (expect string)HI, i m usin openAPi I declared return params in my route ``` const getUserRoute = createRoute({ Deploying Hono App to Multiple RuntimesHow can I deploy my Hono app to multiple different runtimes simultaneously within the same project? Sourcemap Support for Sentry MiddlewareThere's some way to improve the DX of using Sentry with Hono? I'm receiving the issues on my Sentry Does Hono Cloudflare Pages work with `@supabase/supabase-js`?## Logs These were my errors on `bun run dev`, full logs attached. ```bash 9:21:29 PM [vite] Error wWelcome To The Hono Help ForumWelcome to the dedicated forum for Hono help. This platform is designed to ensure your questions abo