diff --git a/server/routes.ts b/server/routes.ts index 0b8c1b1..504996c 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -323,7 +323,7 @@ export async function registerRoutes(app: Express): Promise { try { // Check if the API is working by hitting the version endpoint - const response = await axios.get("https://apiv4.iucnredlist.org/api/v4/version", { + const response = await axios.get("https://apiv4.iucnredlist.org/api/v4/information/api_version", { headers: { "Authorization": `Bearer ${iucnToken}` } @@ -558,9 +558,16 @@ export async function registerRoutes(app: Express): Promise { const nameParts = String(name).split(' '); const [genusName, speciesName] = nameParts; - // Get IUCN token - const activeToken = await storage.getActiveToken(); - if (!activeToken?.iucnToken) { + // Try to get IUCN token from environment variable first + let iucnToken = process.env.IUCN_API_KEY || null; + + // If not available in env, try to get from storage + if (!iucnToken) { + const activeToken = await storage.getActiveToken(); + iucnToken = activeToken?.iucnToken || null; + } + + if (!iucnToken) { return res.status(401).json({ success: false, message: "IUCN API v4 token is not configured. Please set your token in the API Token panel." @@ -571,7 +578,7 @@ export async function registerRoutes(app: Express): Promise { // First, we need to find the species taxon ID using scientific name lookup const taxaResponse = await axios.get("https://apiv4.iucnredlist.org/api/v4/taxa/scientific_name", { headers: { - "Authorization": `Bearer ${activeToken.iucnToken}` + "Authorization": `Bearer ${iucnToken}` }, params: { genus_name: genusName, @@ -592,7 +599,7 @@ export async function registerRoutes(app: Express): Promise { // Now retrieve the habitats using the taxon ID const habitatsResponse = await axios.get(`https://apiv4.iucnredlist.org/api/v4/habitats`, { headers: { - "Authorization": `Bearer ${activeToken.iucnToken}` + "Authorization": `Bearer ${iucnToken}` }, params: { taxonid: taxonId @@ -649,9 +656,16 @@ export async function registerRoutes(app: Express): Promise { const nameParts = String(name).split(' '); const [genusName, speciesName] = nameParts; - // Get IUCN token - const activeToken = await storage.getActiveToken(); - if (!activeToken?.iucnToken) { + // Try to get IUCN token from environment variable first + let iucnToken = process.env.IUCN_API_KEY || null; + + // If not available in env, try to get from storage + if (!iucnToken) { + const activeToken = await storage.getActiveToken(); + iucnToken = activeToken?.iucnToken || null; + } + + if (!iucnToken) { return res.status(401).json({ success: false, message: "IUCN API v4 token is not configured. Please set your token in the API Token panel." @@ -662,7 +676,7 @@ export async function registerRoutes(app: Express): Promise { // First, we need to find the species taxon ID using scientific name lookup const taxaResponse = await axios.get("https://apiv4.iucnredlist.org/api/v4/taxa/scientific_name", { headers: { - "Authorization": `Bearer ${activeToken.iucnToken}` + "Authorization": `Bearer ${iucnToken}` }, params: { genus_name: genusName, @@ -683,7 +697,7 @@ export async function registerRoutes(app: Express): Promise { // Now retrieve the conservation measures using the taxon ID const measuresResponse = await axios.get(`https://apiv4.iucnredlist.org/api/v4/measures`, { headers: { - "Authorization": `Bearer ${activeToken.iucnToken}` + "Authorization": `Bearer ${iucnToken}` }, params: { taxonid: taxonId