Getting Started with Mapbox GL JS

Understand how Mapbox products fit together, configure an access token, and create your first interactive map.

🎯 Introduction

By the end of this article, you will be able to create your first Mapbox map on a web page and troubleshoot initialization failures caused by access tokens, container dimensions, or WebGL.


🧱 What Mapbox GL JS does

Mapbox GL JS is a JavaScript library for rendering interactive maps in the browser with WebGL. Use it to display basemaps, overlay point, line, and polygon data, change the camera view, and respond to clicks, zooming, and other interactions.

Here is how the main Mapbox products and resources fit together:

NamePurpose
Mapbox GL JSLoad, render, and interact with maps on web pages
Mapbox StudioCreate map styles and manage data resources online
StyleJSON configuration describing the basemap, fonts, colors, and layers
TilesetData organized into tiles that the map loads for the current extent

The map instance is the main entry point. A style defines how the map looks; a source supplies data; a layer renders that data as circles, lines, fills, or text; and controls provide interface elements such as zoom buttons and a scale bar.


🔐 Configure an access token

An access token lets Mapbox identify your project and determine which resources it can access. Mapbox GL JS loads styles, tiles, and fonts in the browser, so a public token is sent with network requests. It serves a different purpose from a server-side secret.

Before creating the map, assign the public token from your project configuration to mapboxgl.accessToken. This course uses Vite environment variables; if you use another build tool, replace only the code that reads the token.

.env.local
VITE_MAPBOX_TOKEN=your_public_token
main.js
mapboxgl.accessToken = import.meta.env.VITE_MAPBOX_TOKEN

Add .env.local to .gitignore to keep local configuration out of the repository. Environment variables do not make a public token secret in the browser: they avoid hardcoding it in source files and separate development, testing, and production settings. Also restrict the token's allowed URLs and permissions on the Mapbox Access Tokens page.

The Mapbox dashboard changes periodically, so button labels and navigation may differ from what you see. Treat these steps as a guide and follow the current interface.

📦 Install and create a map

The examples in this course use Node.js 22.9.0 and mapbox-gl@3.14.0. In a new project, run:

pnpm add mapbox-gl@3.14.0
index.html
<div id="map"></div>
style.css
#map {
    width: 100%;
    height: 280px;
}
main.js
import mapboxgl from 'mapbox-gl'
import 'mapbox-gl/dist/mapbox-gl.css'

mapboxgl.accessToken = import.meta.env.VITE_MAPBOX_TOKEN

const map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/light-v11',
    center: [116.397, 39.909],
    zoom: 10,
})

container also accepts a DOM element. Mapbox inserts its canvas into this container. If the container has a height of 0, the instance may exist even though the map is invisible. Import the CSS as well, or controls and popups will not display correctly.

Here is the map rendered by this initialization code:


⚠️ Troubleshoot initialization failures

  • 401 or 403 in the console: check the token, permissions, and URL restrictions.
  • A blank area on the page: check that the container has explicit dimensions.
  • Failed style or tile requests: check the network, style URL, and token permissions.
  • Unable to create a WebGL context: update the browser and graphics driver, and check whether hardware acceleration is disabled.
  • window is not defined in a server-rendered framework: create the map instance only after the code runs in the browser.

🧾 Summary

  • Mapbox GL JS renders interactive maps in the browser and provides methods to control them.
  • Styles, sources, layers, and controls define map appearance, supply data, render features, and provide interface controls, respectively.
  • Loading Mapbox resources in the browser requires a public access token.
  • Give the map container explicit dimensions and create the instance in the browser.
  • Tokens, WebGL, network issues, and container dimensions are common causes of initialization failures.

❓ Questions and answers

Q1: Why can't a public token be completely hidden in frontend code?

The map SDK requests styles and tiles from the browser and sends the public token with those requests. Environment variables avoid hardcoding and distinguish environments; access control still depends on the token's URL restrictions and permissions.

Q2: Are Mapbox Studio and Mapbox GL JS the same tool?

No. Studio creates and manages resources, while Mapbox GL JS loads and interacts with those resources on web pages.

Q3: Can I import the JavaScript without the CSS?

The basemap may appear, but controls, markers, and popups will lack essential styles. Import both the JavaScript and CSS.


🧪 Exercises

  1. Set the center to a city you know, keeping longitude before latitude.
  2. Temporarily remove the container height, inspect the page and console, then restore it.
// Write your code here

🎉 You now know how to initialize a Mapbox GL JS map!