Skip to content

Guide

Plant toxicity in JavaScript

Updated:

Short answer

Call https://api.plant-toxicity-api.com/v1/toxicity with fetch from Node.js 18 or later, passing the plant name and your key in the X-API-Key header. Do it on your server, never in the browser: the example puts a small endpoint in front of the API that keeps the key secret and caches answers.

A small client

// plant-api.mjs
const API = 'https://api.plant-toxicity-api.com/v1';
const KEY = process.env.PTA_KEY; // pk_..., from the dashboard

export async function toxicity(name, lang = 'en') {
  const url = `${API}/toxicity?${new URLSearchParams({ name, lang })}`;
  const res = await fetch(url, { headers: { 'X-API-Key': KEY } });
  if (res.status === 404) return null; // unknown plant
  const body = await res.json();
  if (!res.ok) {
    const e = new Error(`${res.status} ${body.error?.code}: ${body.error?.message}`);
    e.retryAfter = res.headers.get('Retry-After');
    throw e;
  }
  return body.data;
}
import { toxicity } from './plant-api.mjs';

const t = await toxicity('Easter lily');
console.log(t.cat.level, t.cat.summary.en); // emergency Listed as able to cause serious poisoning.
console.log(t.dog.level, t.dog.summary.en); // not-rated On 1 of the 7 poison lists checked, not enough for a rating. Treat with caution.

An endpoint for your front end

This Express route keeps the key on the server, caches each name for a day and returns the fields your page needs.

// server.mjs
import express from 'express';
import { toxicity } from './plant-api.mjs';

const app = express();
const cache = new Map();
const DAY = 24 * 3600 * 1000;

app.get('/api/plant-toxicity', async (req, res) => {
  const name = String(req.query.name ?? '').trim();
  if (!name) return res.status(400).json({ error: 'name is required' });
  const key = name.toLowerCase();
  const hit = cache.get(key);
  if (hit && Date.now() - hit.at < DAY) return res.json(hit.data);
  try {
    const t = await toxicity(name);
    const data = t && {
      plant: t.plant.name,
      cat: { level: t.cat.level, summary: t.cat.summary.en },
      dog: { level: t.dog.level, summary: t.dog.summary.en },
      disclaimer: t.disclaimer.en,
    };
    cache.set(key, { at: Date.now(), data });
    res.json(data);
  } catch (e) {
    res.status(502).json({ error: 'lookup failed' });
  }
});

app.listen(3000);

You may cache for up to 30 days; one day keeps the data fresh after a new data release.

In the browser

const res = await fetch(`/api/plant-toxicity?name=${encodeURIComponent('Monstera')}`);
const t = await res.json();
badge.textContent = t ? `Cats: ${t.cat.summary}` : 'Plant not found';

Next to the badge, show your own notice that the data is general information and not veterinary advice (Terms, section 4); the disclaimer sentence can be part of it. The levels, from weakest to strictest, are not-rated, non-toxic, listed, mild, toxic and emergency; see the methodology.

Next steps

  • Care plans: GET /care?name= returns light, water, soil and feeding (plant care API).
  • Many plants: POST /batch with { "items": [{ "op": "toxicity", "name": "..." }] } on paid plans.
  • Types: generate them from /openapi.json.

Questions

Can I call the API directly from the browser?

Not with your key. Anything in front-end code can be read, and someone could use your credits. Put a small endpoint on your server, as in this guide, and call that from the browser.

Is there a JavaScript SDK?

Not yet. The API is plain JSON over HTTPS, so fetch is enough. The OpenAPI spec at /openapi.json works with generators such as openapi-typescript if you want types.

Which Node.js version do I need?

Node.js 18 or later, which has fetch built in.