Guide
Pet toxicity badges on Shopify
Updated:
Short answer
Store each plant's toxicity in Shopify product metafields and show it with a short Liquid snippet. A small Node.js script reads the plant name of each product, asks Plant Toxicity API for the cat and dog ratings, and writes them with the Admin API; the theme reads nothing but the metafields, so your API key never reaches the storefront.
What you need
- A Shopify custom app with the Admin API scopes
read_productsandwrite_products, and its Admin API access token. - A product metafield with the plant’s name, for example
custom.plant_name(“Monstera deliciosa” or “Easter lily”). - Two metafield definitions for the result:
custom.cat_toxicityandcustom.dog_toxicity, type single line text. - A Plant Toxicity API key. One credit per product per run; 500 credits a month are free.
1. Fetch the ratings and save them
// sync-toxicity.mjs (Node.js 18+): node sync-toxicity.mjs
const SHOP = process.env.SHOP; // your-store.myshopify.com
const TOKEN = process.env.SHOPIFY_TOKEN; // Admin API access token
const PTA_KEY = process.env.PTA_KEY; // pk_...
const VERSION = '2025-10'; // a current Admin API version
async function admin(query, variables) {
const res = await fetch(`https://${SHOP}/admin/api/${VERSION}/graphql.json`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-Shopify-Access-Token': TOKEN },
body: JSON.stringify({ query, variables }),
});
const body = await res.json();
if (body.errors) throw new Error(JSON.stringify(body.errors));
return body.data;
}
async function toxicity(name) {
const url = `https://api.plant-toxicity-api.com/v1/toxicity?${new URLSearchParams({ name })}`;
const res = await fetch(url, { headers: { 'X-API-Key': PTA_KEY } });
if (res.status === 404) return null;
if (!res.ok) throw new Error(`${res.status} for ${name}`);
return (await res.json()).data;
}
let cursor = null;
do {
const { products } = await admin(
`query($cursor: String) {
products(first: 50, after: $cursor) {
pageInfo { hasNextPage endCursor }
nodes { id title plant: metafield(namespace: "custom", key: "plant_name") { value } }
}
}`,
{ cursor },
);
for (const p of products.nodes) {
if (!p.plant?.value) continue;
const t = await toxicity(p.plant.value);
if (!t) { console.log('Unknown plant:', p.title, '/', p.plant.value); continue; }
await admin(
`mutation($m: [MetafieldsSetInput!]!) { metafieldsSet(metafields: $m) { userErrors { field message } } }`,
{ m: ['cat', 'dog'].map((a) => ({ ownerId: p.id, namespace: 'custom', key: `${a}_toxicity`, type: 'single_line_text_field', value: t[a].level })) },
);
console.log(p.title, 'cat:', t.cat.level, 'dog:', t.dog.level);
}
cursor = products.pageInfo.hasNextPage ? products.pageInfo.endCursor : null;
} while (cursor);
2. Show the badges
Add a snippet snippets/pet-toxicity.liquid and render it on the product template with {% render 'pet-toxicity', product: product %}.
{%- assign warn = 'emergency,toxic,mild,listed,not-rated' | split: ',' -%}
{%- assign animals = 'cat,dog' | split: ',' -%}
{%- for animal in animals -%}
{%- assign key = animal | append: '_toxicity' -%}
{%- assign level = product.metafields.custom[key].value -%}
{%- if level and warn contains level -%}
<span class="pet-badge pet-badge--{{ level }}">
{%- if level == 'not-rated' -%}{{ animal | capitalize }}s: not enough data, treat with caution
{%- else -%}Toxic to {{ animal }}s ({{ level }}){%- endif -%}
</span>
{%- endif -%}
{%- endfor -%}
<p class="pet-badge-note">For information only, not a substitute for a vet or poison center. If you think your pet has eaten a plant, contact your vet or an animal poison hotline at once.</p>
Style the levels in your theme CSS, for example a red badge for emergency and toxic, amber for mild and listed, grey for not-rated.
Good to know
- Keep a notice under the badges: the Terms (section 4) ask you to show your own notice that the data is general information, not veterinary advice, and to contact a vet or poison centre in case of doubt. The note in the snippet uses the API’s
disclaimersentence as part of it. - On the Free plan, add “Plant data by Plant Toxicity API” with a link to https://plant-toxicity-api.com near the badges.
non-toxicmeans a named source lists the plant as non-toxic and none of the poison lists checked names it; the snippet shows no badge for it. Do not turn it into a “pet friendly” claim; see the methodology.
Questions
Why not call the API from the theme?
Theme code runs in the shopper's browser, so the key would be public. Metafields also make pages faster, because the rating is already on the product.
How often should I run the script?
When you add plants, and about once a month for the rest; you may keep the data for up to 30 days. Each product uses one credit per run.
What if a product is not a plant?
The script skips products without the plant name metafield. Products with a name the API does not know get no badge and are listed in the script output.