How to access env variables?

I set one using wrangler and now I can't access it? I couldn't even find a way to access it in the docs help me.
No description
19 Replies
Hello, I’m Allie!
You need to pass the context in when using env() https://hono.dev/helpers/adapter#env
Adapter Helper - Hono
Ultrafast web framework for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.
avglinuxguy
avglinuxguyOP10mo ago
https://github.com/honojs/hono/issues/799#issuecomment-1990977003 Can you check this? I'm struggling to figure it out.
GitHub
.env files not loaded into c.env · Issue #799 · honojs/hono
I found the .env files are not loaded into c.env: Runtime: Bun 0.4.0 // index.ts import { Hono } from 'hono' interface Bindings { API_KEY: string API_SECRET: string } const app = new Hono&l...
Hello, I’m Allie!
You are extracting GEMINI_AI, but then trying to use GEMINI_API
avglinuxguy
avglinuxguyOP10mo ago
I corrected it Is there a way to use it wihout using (c)? The thread it corrected, read one it GEMINI_API
Hello, I’m Allie!
No, you are supposed to use c Environment Variables are not accessible in a global context
avglinuxguy
avglinuxguyOP10mo ago
import { Hono } from 'hono';
import { env } from 'hono/adapter';

const app = new Hono();

app.get('/', (c) => {

const {GEMINI_AI} = env(c)
console.log("my key:" + GEMINI_API)
return c.json({
message: 'Live!',
env: GEMINI_API
})
})

export default app;
import { Hono } from 'hono';
import { env } from 'hono/adapter';

const app = new Hono();

app.get('/', (c) => {

const {GEMINI_AI} = env(c)
console.log("my key:" + GEMINI_API)
return c.json({
message: 'Live!',
env: GEMINI_API
})
})

export default app;
Ok, so in this code I want to access the env outsite the get? so ho do I do that?
Hello, I’m Allie!
You don't. Environment variables are only defined within the get Or whatever other handler functions you define
avglinuxguy
avglinuxguyOP10mo ago
Ok, so I can do this right? let output = await run(c); ?? Like I need the value of env in my run()
Hello, I’m Allie!
Yes, you can do that
avglinuxguy
avglinuxguyOP10mo ago
import { Hono } from 'hono'
import { env, getRuntimeKey } from 'hono/adapter'
import { GoogleGenerativeAI } from '@google/generative-ai';

const app = new Hono();

async function run(content: any) {
// For text-only input, use the gemini-pro model
const {GEMINI_API} = env(content)
const genAI = new GoogleGenerativeAI(GEMINI_API);
const model = genAI.getGenerativeModel({ model: "gemini-pro"});

const prompt = "Write a story about a magic backpack."

const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
return text
}

app.get('/', async (c) => {

// const {GEMINI_API} = env(c)
const ans = await run(c);
console.log(ans)
console.log("my key:")
return c.json({
message: 'Live!',
Quote: ans
})
})


export default app;
import { Hono } from 'hono'
import { env, getRuntimeKey } from 'hono/adapter'
import { GoogleGenerativeAI } from '@google/generative-ai';

const app = new Hono();

async function run(content: any) {
// For text-only input, use the gemini-pro model
const {GEMINI_API} = env(content)
const genAI = new GoogleGenerativeAI(GEMINI_API);
const model = genAI.getGenerativeModel({ model: "gemini-pro"});

const prompt = "Write a story about a magic backpack."

const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
return text
}

app.get('/', async (c) => {

// const {GEMINI_API} = env(c)
const ans = await run(c);
console.log(ans)
console.log("my key:")
return c.json({
message: 'Live!',
Quote: ans
})
})


export default app;
Am I doing it right, it's working, but sometimes give internal server error.
Hello, I’m Allie!
Looks right to me. What does the error show?
avglinuxguy
avglinuxguyOP10mo ago
Just internal server error is there a console on on Cloudflare, let me check Now, After the second deployment the errors vanished now no more errors.
avglinuxguy
avglinuxguyOP10mo ago
No description
Hello, I’m Allie!
Does it work locally?
avglinuxguy
avglinuxguyOP10mo ago
Yes, it working fine now. Just a little slow I will enable streaming now, so it gives words one by one. Let's see how can I do that
avglinuxguy
avglinuxguyOP10mo ago
No description
avglinuxguy
avglinuxguyOP10mo ago
I didn't had cors in it, so was working with my IP only. Couldn't open the site from anyohter place
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { env, getRuntimeKey } from 'hono/adapter'
import { GoogleGenerativeAI } from '@google/generative-ai';

const app = new Hono();

app.use('/*', cors())

async function run(content: any) {
// For text-only input, use the gemini-pro model
const {GEMINI_API} = env(content)
const genAI = new GoogleGenerativeAI(GEMINI_API);
const model = genAI.getGenerativeModel({ model: "gemini-pro"});

const prompt = "Write a line."

const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
return text
}

app.get('/', async (c) => {

const ans = await run(c);
return c.json({
message: 'Live!',
Quote: ans
})
})

app.get('/v1', async (c) => {

// const ans = await run(c);
return c.text("ans")
})


export default app;
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { env, getRuntimeKey } from 'hono/adapter'
import { GoogleGenerativeAI } from '@google/generative-ai';

const app = new Hono();

app.use('/*', cors())

async function run(content: any) {
// For text-only input, use the gemini-pro model
const {GEMINI_API} = env(content)
const genAI = new GoogleGenerativeAI(GEMINI_API);
const model = genAI.getGenerativeModel({ model: "gemini-pro"});

const prompt = "Write a line."

const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
return text
}

app.get('/', async (c) => {

const ans = await run(c);
return c.json({
message: 'Live!',
Quote: ans
})
})

app.get('/v1', async (c) => {

// const ans = await run(c);
return c.text("ans")
})


export default app;
I have this code, and I can't access it outside my home network. I don't know why /v1 route is working but not the / route ANS: it's not wokers it GEMINI API: giving this output. "Error: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent: [400 Bad Request] User location is not supported for the API use."
Hello, I’m Allie!
That sounds like an issue from Google’s end They might have locked their APIs down to certain locations by IP
avglinuxguy
avglinuxguyOP10mo ago
It works with my HOME network but not with the mobile LOL What a pain. Need to find some other free LLM api Nope, finally I found the issue was with the way CloudFlare handled the reqeust, since Google is geo-blocking in some countries, When I was using fiber internet it worked, but when I switched to mobile internet I don't know how cloudflare did it's routing, but it might have went to some geo-blocked country like EU etc. But the logs always showed some IP from India. Using the VPN worked.
Want results from more Discord servers?
Add your server