From 052f9fbbf0ce3105ab1089b476204e808ff90f5d Mon Sep 17 00:00:00 2001 From: Magnus-SmariSma <20734986-Magnus-SmariSma@users.noreply.replit.com> Date: Thu, 20 Mar 2025 23:27:41 +0000 Subject: [PATCH] Refactor: Prioritize IUCN API key from environment variables. Replit-Commit-Author: Agent Replit-Commit-Session-Id: e931b5ab-041b-42e7-baf1-50017869cef6 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/e19c6a51-7e4c-4bb8-a6a6-46dc00f0ec99/96599b53-5e69-4e62-a096-dfed59479efa.jpg --- server/routes.ts | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/server/routes.ts b/server/routes.ts index eacc850..0b8c1b1 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -304,9 +304,16 @@ export async function registerRoutes(app: Express): Promise { // IUCN API Status check endpoint - v4 only app.get("/api/iucn/status", async (req: Request, res: Response) => { try { - const activeToken = await storage.getActiveToken(); + // First try to use environment variable for API key + let iucnToken = process.env.IUCN_API_KEY || null; - if (!activeToken?.iucnToken) { + // 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, connected: false, @@ -318,10 +325,12 @@ export async function registerRoutes(app: Express): Promise { // Check if the API is working by hitting the version endpoint const response = await axios.get("https://apiv4.iucnredlist.org/api/v4/version", { headers: { - "Authorization": `Bearer ${activeToken.iucnToken}` + "Authorization": `Bearer ${iucnToken}` } }); + console.log("IUCN API v4 version check response:", response.data); + return res.json({ success: true, connected: true, @@ -373,9 +382,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." @@ -386,7 +402,7 @@ export async function registerRoutes(app: Express): Promise { // Use the v4 API with scientific name endpoint const response = await axios.get("https://apiv4.iucnredlist.org/api/v4/taxa/scientific_name", { headers: { - "Authorization": `Bearer ${activeToken.iucnToken}` + "Authorization": `Bearer ${iucnToken}` }, params: { genus_name: genusName, @@ -444,9 +460,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." @@ -457,7 +480,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, @@ -478,7 +501,7 @@ export async function registerRoutes(app: Express): Promise { // Now retrieve the threats using the taxon ID const threatsResponse = await axios.get(`https://apiv4.iucnredlist.org/api/v4/threats`, { headers: { - "Authorization": `Bearer ${activeToken.iucnToken}` + "Authorization": `Bearer ${iucnToken}` }, params: { taxonid: taxonId