Add IUCN API integration and status check endpoint; improve error handling and limit API request parameters to avoid 414 errors.

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/5e1b7244-581f-4557-8231-e696f4d2199f.jpg
This commit is contained in:
Magnus-SmariSma
2025-03-20 22:35:35 +00:00
parent 859c5da9a0
commit 4e15b5f2c0
3 changed files with 442 additions and 4 deletions

View File

@ -0,0 +1,378 @@
API V4
v4
OAS 3.0
/api-docs/v4/openapi.yaml
Authorize
Assessment
Return assessment data relating to a specific assessment_id. You do not need to supply a latest assessment_id; you can therefore return data on latest and historic assessments if the assessment_id is known. You can gather a list of current and historic assessment_id for a specific species by using api/v4/taxa/scientific_name/{scientific_name}
GET
/api/v4/assessment/{assessment_id}
Retrieves an assessment
Biogeographical Realms
Return the latest assessments for a biogeographical realm.
GET
/api/v4/biogeographical_realms/
Returns a list of biogeographic realm codes
GET
/api/v4/biogeographical_realms/{code}
Returns a collection of assessments for a biogeographical realm code
Comprehensive Groups
Return the latest assessments for a comprehensive group
GET
/api/v4/comprehensive_groups/
Returns a list of comprehensive groups
GET
/api/v4/comprehensive_groups/{name}
Returns a collection of assessments for a comprehensive group name
Conservation Actions
Description coming soon...
GET
/api/v4/conservation_actions/
returns a list of conservation actions
GET
/api/v4/conservation_actions/{code}
returns a collection of assessments for a conservation action code
Countries
Return the latest assessments for a given country.
GET
/api/v4/countries/
Returns a list of countries by ISO alpha-2 code
GET
/api/v4/countries/{code}
Returns a collection of assessments for a given country ISO alpha-2 code
FAOs
Documentation coming soon..
GET
/api/v4/faos/
Returns a list of FAOs
GET
/api/v4/faos/{code}
Returns a collection of assessments for an FAO code
Growth Forms
Return the latest assessments for a given growth form.
GET
/api/v4/growth_forms/
Returns a list of growth forms
GET
/api/v4/growth_forms/{code}
Returns a collection of assessments for a given growth form code
Green Status
Return the latest Green Status assessments.
GET
/api/v4/green_status/all
Returns a list of all Green Status assessments
Habitats
Return the latest assessments for a given habitat (e.g. Forest - Temperate or Marine Intertidal). These habitat codes correspond to the IUCN Red List Habitats Classification Scheme (v3.1)
GET
/api/v4/habitats/
Returns a list of habitat codes.
GET
/api/v4/habitats/{code}
Returns a collection of assessments for a given habitat code
Information
Endpoints for supplying general API-specific information.
GET
/api/v4/information/api_version
Returns the current version number of the IUCN Red List of Threatened Species API
GET
/api/v4/information/red_list_version
Returns the current IUCN Red List of Threatened Species version
Population Trends
Return a list of the latest assessments based on a population trend (i.e. increasing, decreasing stable or unknown)
GET
/api/v4/population_trends/
Returns a list of population trends
GET
/api/v4/population_trends/{code}
Returns a collection of assessments for a given population trend code
Red List Categories
Return a list of the latest assessments for a given category. (Not Evaluated, Data Deficient, Least Concern, Near Threatened, Vulnerable, Endangered, Critically Endangered, Extinct in the Wild and Extinct).
GET
/api/v4/red_list_categories/
Returns a list of Red List categories
GET
/api/v4/red_list_categories/{code}
Returns a collection of assessments for a given Red List category code
Research
Return a list of the latest assessments for a given research category.
GET
/api/v4/research/
Returns a list of habitat codes.
GET
/api/v4/research/{code}
Returns a collection of assessments for a given research code
Scopes
Return latest assessments for a given geographical assessment scope (e.g. Global, Mediterranean)
GET
/api/v4/scopes/
returns a list of scopes
GET
/api/v4/scopes/{code}
Returns a collection of assessments for a given scope code
Statistics
Return aggregated statistics.
GET
/api/v4/statistics/count
Return count of the number of species with assessments
Stresses
Return a list of the latest assessments based on the stresses species are subject to (e.g. Ecosystem degradation, species disturbance etc.)
GET
/api/v4/stresses/
Returns a list of stressors
GET
/api/v4/stresses/{code}
returns a collection of assessments for a given stress code
Systems
Return assessments belonging to broad systems (e.g. terrestrial, freshwater or marine)
GET
/api/v4/systems/
Returns a list of systems
GET
/api/v4/systems/{code}
Returns a collection of assessments for a given system code
Taxa
Return summary assessment data at the species level and other higher taxonomic levels (kingdom to family, inclusive). When querying at the species level, the API returns the latest and historic assessment information.
GET
/api/v4/taxa/sis/{sis_id}
Returns a collection of assessments for a given SIS id
GET
/api/v4/taxa/scientific_name
Returns a collection of assessments for a given genus_name and species_name (i.e. Latin binomial) and optional infra_name (i.e. Latin trinomial)
GET
/api/v4/taxa/kingdom/
Returns a list of all kingdom names
GET
/api/v4/taxa/kingdom/{kingdom_name}
Returns a collection of the latests assessments for a given kingdom_name
GET
/api/v4/taxa/phylum/
Returns a list of all phylum names
GET
/api/v4/taxa/phylum/{phylum_name}
Returns a collection of the latests assessments for a given phylum_name
GET
/api/v4/taxa/class/
Returns a list of all class names
GET
/api/v4/taxa/class/{class_name}
Returns a collection of the latests assessments for a given class_name
GET
/api/v4/taxa/order/
Returns a list of all order names
GET
/api/v4/taxa/order/{order_name}
Returns a collection of the latests assessments for a given order_name
GET
/api/v4/taxa/family/
Returns a list of all family names
GET
/api/v4/taxa/family/{family_name}
Returns a collection of the latests assessments for a given family_name
GET
/api/v4/taxa/possibly_extinct
Returns a collection of the all latest global assessments for taxa that are possibly extinct
GET
/api/v4/taxa/possibly_extinct_in_the_wild
Returns a collection of the all latest global assessments for taxa that are possibly extinct in the wild
Threats
Return a list of the latest assessments which are subject to a specific threat (e.g. energy production and mining, climate change and severe weather)
GET
/api/v4/threats/
Returns a list of threats
GET
/api/v4/threats/{code}
Returns a collection of assessments for a given threat code
Use and Trade
Return a list of the latest assessments which are subject to a specific uses and trade.
GET
/api/v4/use_and_trade/
Returns a list of use and trades
GET
/api/v4/use_and_trade/{code}
Returns a collection of assessments for a given use and t

View File

@ -18,6 +18,7 @@ export default function Home() {
const { data: tokenData } = useQuery({
queryKey: ['/api/token'],
queryFn: async () => await apiClient.getToken(),
initialData: null,
});
// Get recent searches

View File

@ -213,6 +213,48 @@ export async function registerRoutes(app: Express): Promise<Server> {
}
});
// IUCN API Status check endpoint
app.get("/api/iucn/status", async (req: Request, res: Response) => {
try {
const apiKey = process.env.IUCN_API_KEY;
if (!apiKey) {
return res.status(401).json({
success: false,
message: "IUCN API key is not configured"
});
}
// Use a region endpoint which is simpler and less prone to errors
try {
const response = await axios.get(`${IUCN_BASE_URL}/region/list`, {
params: { token: apiKey }
});
res.json({
success: true,
connected: true,
message: "IUCN API is connected and responding"
});
} catch (error) {
if (axios.isAxiosError(error) && error.response) {
return res.status(error.response.status).json({
success: false,
connected: false,
message: "Failed to connect to IUCN API",
status: error.response.status
});
}
throw error;
}
} catch (error) {
res.status(500).json({
success: false,
connected: false,
message: "Error checking IUCN API status"
});
}
});
// IUCN Red List API routes
app.get("/api/iucn/species", async (req: Request, res: Response) => {
try {
@ -233,8 +275,13 @@ export async function registerRoutes(app: Express): Promise<Server> {
});
}
// To avoid 414 errors, we'll limit the name parameter to just the genus and species
// This will extract first two parts of scientific name (genus and species)
const nameParts = String(name).split(' ');
const simplifiedName = nameParts.slice(0, 2).join(' ');
try {
const response = await axios.get(`${IUCN_BASE_URL}/species/name/${name}`, {
const response = await axios.get(`${IUCN_BASE_URL}/species/name/${encodeURIComponent(simplifiedName)}`, {
params: { token: apiKey }
});
@ -279,8 +326,12 @@ export async function registerRoutes(app: Express): Promise<Server> {
});
}
// To avoid 414 errors, we'll limit the name parameter to just the genus and species
const nameParts = String(name).split(' ');
const simplifiedName = nameParts.slice(0, 2).join(' ');
try {
const response = await axios.get(`${IUCN_BASE_URL}/threats/species/name/${name}`, {
const response = await axios.get(`${IUCN_BASE_URL}/threats/species/name/${encodeURIComponent(simplifiedName)}`, {
params: { token: apiKey }
});
@ -325,8 +376,12 @@ export async function registerRoutes(app: Express): Promise<Server> {
});
}
// To avoid 414 errors, we'll limit the name parameter to just the genus and species
const nameParts = String(name).split(' ');
const simplifiedName = nameParts.slice(0, 2).join(' ');
try {
const response = await axios.get(`${IUCN_BASE_URL}/habitats/species/name/${name}`, {
const response = await axios.get(`${IUCN_BASE_URL}/habitats/species/name/${encodeURIComponent(simplifiedName)}`, {
params: { token: apiKey }
});
@ -371,8 +426,12 @@ export async function registerRoutes(app: Express): Promise<Server> {
});
}
// To avoid 414 errors, we'll limit the name parameter to just the genus and species
const nameParts = String(name).split(' ');
const simplifiedName = nameParts.slice(0, 2).join(' ');
try {
const response = await axios.get(`${IUCN_BASE_URL}/measures/species/name/${name}`, {
const response = await axios.get(`${IUCN_BASE_URL}/measures/species/name/${encodeURIComponent(simplifiedName)}`, {
params: { token: apiKey }
});