From 47acadc88c51fb880bb223b60a51c45bdc1bdfa1 Mon Sep 17 00:00:00 2001 From: Christopher Mayor Date: Wed, 29 Apr 2026 15:43:40 +0000 Subject: [PATCH] Add Yelp Fusion API proxy endpoint for dynamic map search --- server.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/server.js b/server.js index b81449d..52661c3 100644 --- a/server.js +++ b/server.js @@ -360,6 +360,62 @@ app.post('/api/polls', (req, res) => { res.json({ success: true, pollsOpen: data.pollsOpen }); }); +// ── Yelp Fusion API proxy ─────────────────────────────────── +// API key lives server-side — never exposed to the browser. +// Get your free key at: https://www.yelp.com/developers +const YELP_API_KEY = process.env.YELP_API_KEY || ''; + +app.get('/api/yelp', async (req, res) => { + const { term, location } = req.query; + if (!term) return res.status(400).json({ error: 'term is required' }); + + if (!YELP_API_KEY) { + return res.status(503).json({ + error: 'YELP_API_KEY not configured on server. Add it as an environment variable.' + }); + } + + try { + const params = new URLSearchParams({ + term: term + ' in ' + location, + location: location || 'Los Cabos Mexico', + limit: '15', + sort_by: 'rating', + categories: 'restaurants,nightlife,active,arts,health', + }); + const response = await fetch(`https://api.yelp.com/v3/businesses/search?${params}`, { + headers: { + 'Authorization': `Bearer ${YELP_API_KEY}`, + 'Accept': 'application/json', + } + }); + + if (!response.ok) { + const errText = await response.text(); + return res.status(response.status).json({ error: `Yelp API error: ${errText}` }); + } + + const data = await response.json(); + // Return only the fields we need to keep payload small + const businesses = (data.businesses || []).map(b => ({ + name: b.name, + image_url: b.image_url, + url: b.url, + rating: b.rating, + price: b.price, + coordinates: b.coordinates, + location: b.location, + categories: b.categories, + display_phone: b.display_phone, + distance: b.distance, + })); + res.json({ businesses, total: data.total }); + } catch (err) { + console.error('Yelp proxy error:', err); + res.status(500).json({ error: 'Failed to fetch from Yelp' }); + } +}); + // ── WebSocket ──────────────────────────────────────────────── wss.on('connection', (ws) => {