#How to access env variables?

36 messages · Page 1 of 1 (latest)

mint sleet
#

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.

amber hawk
mint sleet
amber hawk
mint sleet
#

Is there a way to use it wihout using (c)?

mint sleet
amber hawk
#

Environment Variables are not accessible in a global context

mint sleet
#
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?

amber hawk
#

Or whatever other handler functions you define

mint sleet
#

Ok, so I can do this right? let output = await run(c); ??

#

Like I need the value of env in my run()

amber hawk
mint sleet
# amber hawk Yes, you can do that
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.
amber hawk
mint sleet
#

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.

amber hawk
mint sleet
#

Just a little slow

#

I will enable streaming now, so it gives words one by one.

#

Let's see how can I do that

mint sleet
mint sleet
mint sleet
# amber hawk Does it work locally?
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."

amber hawk
#

They might have locked their APIs down to certain locations by IP

mint sleet
#

LOL What a pain.

#

Need to find some other free LLM api

mint sleet
# amber hawk That sounds like an issue from Google’s end

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.