Skip to content

Guide

Plant toxicity in Python

Updated:

Short answer

Send a GET request to https://api.plant-toxicity-api.com/v1/toxicity with the plant name and your key in the X-API-Key header; the JSON has a level for cats, dogs and people with sources. The example on this page uses requests, handles unknown plants and rate limits, and caches answers for up to 30 days, which the terms allow.

Setup

pip install requests
export PTA_KEY="pk_your_key"

One lookup

import os
import requests

API = "https://api.plant-toxicity-api.com/v1"
session = requests.Session()
session.headers["X-API-Key"] = os.environ["PTA_KEY"]


class PlantApiError(Exception):
    pass


def toxicity(name: str, lang: str = "en") -> dict | None:
    r = session.get(f"{API}/toxicity", params={"name": name, "lang": lang}, timeout=10)
    if r.status_code == 404:
        return None  # unknown plant
    if r.status_code == 429:
        raise PlantApiError(f"Rate limit or monthly credits reached, retry after {r.headers.get('Retry-After')} s")
    if not r.ok:
        err = r.json().get("error", {})
        raise PlantApiError(f"{r.status_code} {err.get('code')}: {err.get('message')}")
    return r.json()["data"]


t = toxicity("Easter lily")
for animal in ("cat", "dog", "human"):
    print(animal, t[animal]["level"], "-", t[animal]["summary"]["en"])
print(t["disclaimer"]["en"])

Output for Easter lily:

cat emergency - Listed as able to cause serious poisoning.
dog not-rated - On 1 of the 7 poison lists checked, not enough for a rating. Treat with caution.
human mild - Listed as causing mild symptoms or skin irritation.
For information only, not a substitute for a vet or poison center.

Show the right warning

Levels go from weakest to strictest: not-rated, non-toxic, listed, mild, toxic, emergency. Treat not-rated for cats and dogs as a warning too: its summary says how many of the poison lists checked name the plant (listsNaming and listsChecked give the counts and names) and to treat it with caution.

WARN = {"emergency", "toxic", "mild", "listed", "not-rated"}

def warn_pet_owner(t: dict) -> list[str]:
    return [f"{a}: {t[a]['summary']['en']}" for a in ("cat", "dog") if t[a]["level"] in WARN]

Many plants in one request

def batch(names: list[str]) -> list[dict]:
    items = [{"op": "toxicity", "name": n} for n in names]
    r = session.post(f"{API}/batch", json={"items": items}, timeout=30)
    r.raise_for_status()
    return r.json()["data"]["results"]  # [{ "index": 0, "ok": true, "data": {...} }, ...]

for res in batch(["Monstera deliciosa", "Ficus lyrata", "Nerium oleander"]):
    if res["ok"]:
        print(res["data"]["plant"]["name"], res["data"]["cat"]["level"])

Batch needs a paid plan. op can also be care.

Cache and quota

You may cache responses for up to 30 days. A plain dictionary with a timestamp is enough for a script; use Redis or your database in a web app.

import time

_cache: dict[str, tuple[float, dict | None]] = {}
MAX_AGE = 30 * 24 * 3600

def toxicity_cached(name: str) -> dict | None:
    key = name.strip().lower()
    hit = _cache.get(key)
    if hit and time.time() - hit[0] < MAX_AGE:
        return hit[1]
    data = toxicity(name)
    _cache[key] = (time.time(), data)
    return data

Every response carries X-Quota-Limit, X-Quota-Remaining and X-Quota-Reset, so you can log how many credits are left this month.

On the Free plan

Texts come in English and you show “Plant data by Plant Toxicity API” with a link to https://plant-toxicity-api.com near the data. On every plan, credit the sources in meta.credits and show your own notice that the data is not veterinary or medical advice (Terms, section 4).

Questions

Where do I keep the API key?

In an environment variable or your secret store, on the server. Never put the key in a mobile app or a web page, because anyone could read it and use your credits.

What does a 404 mean?

The API does not know the plant (error code unknown_plant). It still uses a credit. Try the scientific name, or pass family as a fallback.

How do I check many plants at once?

POST /batch with a list of items. Each item uses one credit and the whole request one rate-limit slot. Starter allows 25 items per request, Pro and Business 100.