Token Management Patterns
Environment Variables (Recommended)
Different frameworks use different prefixes for client-side environment variables:
| Framework/Bundler | Environment Variable | Access Pattern |
|---|---|---|
| Vite | VITE_MAPBOX_ACCESS_TOKEN |
import.meta.env.VITE_MAPBOX_ACCESS_TOKEN |
| Next.js | NEXT_PUBLIC_MAPBOX_TOKEN |
process.env.NEXT_PUBLIC_MAPBOX_TOKEN |
| Create React App | REACT_APP_MAPBOX_TOKEN |
process.env.REACT_APP_MAPBOX_TOKEN |
| Angular | environment.mapboxAccessToken |
Environment files (environment.ts) |
Vite .env file:
VITE_MAPBOX_ACCESS_TOKEN=pk.YOUR_MAPBOX_TOKEN_HERENext.js .env.local file:
NEXT_PUBLIC_MAPBOX_TOKEN=pk.YOUR_MAPBOX_TOKEN_HEREImportant:
- Always use environment variables for tokens
- Never commit
.envfiles to version control - Use public tokens (pk.*) for client-side apps
- Add
.envto.gitignore - Provide
.env.exampletemplate for team
.gitignore:
.env
.env.local
.env.*.local.env.example:
VITE_MAPBOX_ACCESS_TOKEN=your_token_hereStyle Configuration
Default Center and Zoom Guidelines
Example defaults (used in create-web-app demos):
- Center:
[-71.05953, 42.36290](Boston, MA) - Zoom:
13for city-level view
Note: GL JS defaults to
center: [0, 0]andzoom: 0if not specified. Always set these explicitly.
Zoom level guide:
0-2: World view3-5: Continent/country6-9: Region/state10-12: City view13-15: Neighborhood16-18: Street level19-22: Building level
Customizing for user location:
// Use browser geolocation
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
map.setCenter([position.coords.longitude, position.coords.latitude]);
map.setZoom(13);
});
}