SDKs & Guides
Node.js Express
Integrate ChowAPI into your Express backend
Add food search, barcode lookup, and nutrition endpoints to your Express.js API.
Prerequisites
- Node.js 18+
- Express.js
- ChowAPI key
Installation
1
Install dependencies
npm install chowapi express2
Set your API key
export CHOWAPI_KEY=chow_live_YOUR_KEYCode examples
Food search route
Create an Express route that wraps ChowAPI search.
server.ts
import express from 'express'
import { ChowAPI } from 'chowapi'
const app = express()
const chow = new ChowAPI(process.env.CHOWAPI_KEY!)
app.get('/api/search', async (req, res) => {
const query = req.query.q as string
if (!query || query.length < 2) {
return res.status(400).json({ error: 'Query too short' })
}
try {
const { results } = await chow.search(query, { limit: 10 })
res.json({ results })
} catch (err) {
res.status(500).json({ error: 'Search failed' })
}
})
app.get('/api/barcode/:code', async (req, res) => {
try {
const food = await chow.barcode(req.params.code)
res.json(food)
} catch (err) {
res.status(404).json({ error: 'Barcode not found' })
}
})
app.listen(3000)Node.js Express Pattern
In-memory caching with node-cache
Cache API responses to reduce ChowAPI calls and improve latency.
server.ts
import NodeCache from 'node-cache'
const cache = new NodeCache({ stdTTL: 300 }) // 5 minute TTL
app.get('/api/search', async (req, res) => {
const query = req.query.q as string
const cacheKey = `search_${query}`
const cached = cache.get(cacheKey)
if (cached) return res.json(cached)
const { results } = await chow.search(query, { limit: 10 })
cache.set(cacheKey, { results })
res.json({ results })
})