can't find TELEGRAM_CHAT_ID even though I specified it
mihrauder
FREEOP

4 months ago

I have a website that forwards requests to Telegram, and for some reason the host doesn't see the ID I specified.

$10 Bounty

12 Replies

Railway
BOT

4 months ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


brody
EMPLOYEE

4 months ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open brody 4 months ago


giriraj673
HOBBY

4 months ago

Could you share a few details? 1. What language are you using? 2. Where did you set the TELEGRAM_CHAT_ID? In the Railway variables tab or a .env file? 3. Can you post the line of code that's trying to read the variable and can you paste the full deployment logs too?


mihrauder
FREEOP

4 months ago

1. Language: TypeScript (Next.js 15, App Router).
2. Yes, on the Railway variables page
3. export async function sendTelegramMessage(message: TelegramMessage) {



  const token = process.env.TELEGRAM_BOT_TOKEN;

  const chatId = message.chatId || process.env.TELEGRAM_CHAT_ID;

  if (!token || !chatId) {

    console.log("[TG] Skipped send (no token/chat)", message.text);

    return { ok: false };

  }

  const url = https://api.telegram.org/bot${token}/sendMessage;

  const res = await fetch(url, {

    method: "POST",

    headers: { "Content-Type": "application/json" },

    body: JSON.stringify({

      chat_id: chatId,

      text: message.text,

      parse_mode: "HTML",

      reply_markup: message.replyMarkup,

      disable_web_page_preview: true,

    }),

    cache: "no-store",

  });

  return res.json();

}


mihrauder
FREEOP

4 months ago

I'll duplicate the errors like this because they're not really visible on the screenshots:npm error path /app

npm error command failed

npm error signal SIGTERM

npm error command sh -c next start

npm error A complete log of this run can be found in: /root/.npm/_logs/2025-09-22T22_03_56_207Z-debug-0.log

[TG] Skipped send (no token/chat) <b>Новая заявка #gpaq61w</b>
install mise packages: node

ERROR: failed to build: failed to solve: secret TELEGRAM_CHAT_ID: not found


mihrauder
FREEOP

4 months ago

I did as you said, still the same error: ERROR: failed to build: failed to solve: secret TELEGRAM_CHAT_ID: not found


mihrauder
FREEOP

4 months ago

In theory, Railway reads variables from its settings, not from ENV files.


samgordon
PRO

4 months ago

In order to load your environmental variables from a file named .env.local you'd need to make sure you configure the dotenv package.

For example:

require('dotenv').config({ path: '.env.local' });

After that you can load environmental variables how you already are.


mihrauder
FREEOP

4 months ago

Why should I download them from ENV if I can do it directly from railway?


brenoepics
TRIAL

4 months ago

ERROR: failed to build: failed to solve: secret TELEGRAM_CHAT_ID: not found

This looks like a build-time error, not a runtime error. Are you sure you’re not referencing it somewhere during the build, such as in next.config.js or in a top-level variable? (asking this because it is not clear where this exported function is being used)

If you do need an env variable at build time, check the Next.js docs: https://nextjs.org/docs/pages/guides/environment-variables#loading-environment-variables-with-nextenv

or just use a .env file as stated before


mihrauder
FREEOP

4 months ago

I checked next.config.ts and it doesn't reference any environment variables. env is only read at runtime in server-side code, such as src/utils/telegram.ts and API routes, not at build time.

Where env is read:

const token = process.env.TELEGRAM_BOT_TOKEN;

const chatId = message.chatId || process.env.TELEGRAM_CHAT_ID;

Key points: Next.js automatically loads .env.local, .env, and .env.production for application code (dev/build/runtime).


mihrauder

I checked next.config.ts and it doesn't reference any environment variables. env is only read at runtime in server-side code, such as src/utils/telegram.ts and API routes, not at build time.Where env is read:const token = process.env.TELEGRAM_BOT_TOKEN;const chatId = message.chatId || process.env.TELEGRAM_CHAT_ID;Key points: Next.js automatically loads .env.local, .env, and .env.production for application code (dev/build/runtime).

uxuz
MODERATOR

4 months ago

Hey, while you mentioned that it is technically only ever used on the server side, there may be the possibility that it is leaked on the client and therefore you will have to fix the leaking/being used on the client issue. You can test this theory by prefixing the environment variable with NEXT_PUBLIC_ as specified in the NextJS docs for client side environment variables, if your app can now read the newly defined environment with the prefix, then it would be a problem with how you use supposedly server side environment variable.

Additionally, while you didn't mention it, if you use Turborepo, then you may have to add additional configuration as specified in the Turborepo docs.


uxuz

Hey, while you mentioned that it is technically only ever used on the server side, there may be the possibility that it is leaked on the client and therefore you will have to fix the leaking/being used on the client issue. You can test this theory by prefixing the environment variable with NEXT_PUBLIC_ as specified in the NextJS docs for client side environment variables, if your app can now read the newly defined environment with the prefix, then it would be a problem with how you use supposedly server side environment variable. Additionally, while you didn't mention it, if you use Turborepo, then you may have to add additional configuration as specified in the Turborepo docs.

mihrauder
FREEOP

4 months ago

I don't quite understand what this is for


Status changed to Open itsrems 4 months ago


Loading...