← Files MapboxARCHIVED FILE
skills/mapbox-search-integration/references/nodejs-search.md
2.13 KB · Oct 2, 2026 · 00:26 UTC
# Node.js: Mapbox Search JS Core
## Option 1: Search JS Core (Recommended)
**When to use:** Server-side search, backend API, serverless functions
**Installation:**
```bash
npm install @mapbox/search-js-core
```
**Complete implementation:**
```javascript
import { SearchSession } from '@mapbox/search-js-core';
// Initialize search session (handles session tokens automatically)
const search = new SearchSession({
accessToken: process.env.MAPBOX_TOKEN
});
// Express.js API endpoint example
app.get('/api/search', async (req, res) => {
const { query, proximity, country } = req.query;
try {
// Get suggestions (Search JS Core handles session management)
const response = await search.suggest(query, {
proximity: proximity ? proximity.split(',').map(Number) : undefined,
country: country,
limit: 10
});
res.json(response.suggestions);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Retrieve full details for a selected result
app.get('/api/search/:id', async (req, res) => {
try {
const result = await search.retrieve(req.params.id);
res.json(result.features[0]);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
```
**Key benefits:**
- ✅ Search JS Core handles session tokens automatically
- ✅ Perfect for serverless (Vercel, Netlify, AWS Lambda)
- ✅ Same API as browser Search JS Core
- ✅ No manual debouncing needed (handle at API gateway level)
## Option 2: Direct API Integration (Advanced)
**When to use:** Very specific requirements, need features not in Search JS Core
**Implementation:**
```javascript
import fetch from 'node-fetch';
async function searchPlaces(query, options = {}) {
const params = new URLSearchParams({
q: query,
access_token: process.env.MAPBOX_TOKEN,
session_token: generateSessionToken(), // You must manage this
...options
});
const response = await fetch(`https://api.mapbox.com/search/searchbox/v1/suggest?${params}`);
return response.json();
}
```
**Important:** Only use direct API calls if Search JS Core doesn't meet your needs. You'll need to handle session tokens manually.
SHA-256: c26c800bb488fc2bc8d66ea44eb5b5759807d5bb309d9a2604a847bfb1874b19