can't read the env variables on my node server (works on local)
mcsrk
HOBBYOP

a year ago

So in my local I have .env and the code reads it perfectly. On railway, neither the project scoped variables or the global shared variables can be readed in the following code: const axios = require("axios");
const { logger } = require("../utils/logger");
const { maskToken } = require("../utils/tokenUtils");

const WHATSAPPAPIVERSION = "v17.0";
const WHATSAPPAPIURL = https://graph.facebook.com/${WHATSAPP_API_VERSION};

// Verificar variables de entorno al inicio
logger.info("Checking WhatsApp environment variables…");

const requiredEnvVars = {
WHATSAPPACCESSTOKEN: process.env.WHATSAPPACCESSTOKEN,
WHATSAPPPHONENUMBERID: process.env.WHATSAPPPHONENUMBERID,
WHATSAPPVERIFYTOKEN: process.env.WHATSAPPVERIFYTOKEN,
};

// Verificar que todas las variables requeridas existan
const missingVars = Object.entries(requiredEnvVars)
// eslint-disable-next-line no-unused-vars
.filter(([_, value]) => !value)
.map(([key]) => key);

if (missingVars.length > 0) {
throw new Error(
Missing required environment variables: ${missingVars.join(", ")}
);
}

// Log seguro de las variables
logger.info(
"WhatsApp configuration:",
JSON.stringify(
{
ACCESSTOKEN: maskToken(requiredEnvVars.WHATSAPPACCESSTOKEN), PHONENUMBERID: requiredEnvVars.WHATSAPPPHONENUMBERID,
VERIFYTOKEN: maskToken(requiredEnvVars.WHATSAPPVERIFY_TOKEN),
},
null,
2
)
);

const sendText = async (to, text) => {
try {
const response = await axios.post(
${WHATSAPP_API_URL}/${requiredEnvVars.WHATSAPP_PHONE_NUMBER_ID}/messages,
{
messagingproduct: "whatsapp", recipienttype: "individual",
to: to,
type: "text",
text: { body: text },
},
{
headers: {
Authorization: Bearer ${requiredEnvVars.WHATSAPP_ACCESS_TOKEN},
"Content-Type": "application/json",
},
}
);

[logger.info](logger.info)(`Message sent successfully to ${to}`);
return [response.data](response.data);

} catch (error) {
logger.error(Error sending WhatsApp message: ${error.message});
throw error;
}
};

const getMediaUrl = async (mediaId) => {
try {
const response = await axios.get(${WHATSAPP_API_URL}/${mediaId}, {
headers: {
Authorization: Bearer ${requiredEnvVars.WHATSAPP_ACCESS_TOKEN},
},
});

[logger.info](logger.info)(`Media URL retrieved for ID: ${mediaId}`);
return [response.data](response.data).url;

} catch (error) {
logger.error(Error getting media URL: ${error.message});
throw error;
}
};

module.exports = {
sendText,
getMediaUrl,
};

View Deploy details

ⓘ Deployment information is only viewable by project members and Railway employees.

1 Replies

a year ago

Hello,

Few issues -

- You where starting your app during build, I removed the incorrect build command.
- All your variables where double quoted, I removed the double quotes.

- Your target port was set incorrectly, I set it to the correct port.

Looks to be online and ruining now!


Loading...