Add an uploaded raster tileset in Leaflet
Add an uploaded raster tileset in Leaflet with L.tileLayer. Fetch its TileJSON, then pass the raster tile template and max zoom. No plugin is needed.
Overview
This guide shows how to add an uploaded raster tileset in Leaflet through TileJSON. Leaflet can render raster XYZ tiles directly with L.tileLayer, so no vector-tile plugin is needed for this raster workflow.
If your tileset starts as an MBTiles, PMTiles, or GeoTIFF file, upload it through the console first. See Upload Data for the full upload workflow, then copy the tileset ID from the Tileset ID column and get an API key from API Keys & Access.
The example uses mappinest.countries-raster, a public sample uploaded raster tileset. It fetches TileJSON first, then passes the returned raster tile template and max zoom to L.tileLayer.
To load your own data, replace mappinest.countries-raster with your tileset ID from the Tileset ID column in the console under Data → Tilesets.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Mappinest raster TileJSON with Leaflet</title>
<link href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" rel="stylesheet" />
<link href="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.css" rel="stylesheet" />
<style>
body { margin: 0; }
#map { height: 100vh; width: 100vw; }
</style>
</head>
<body>
<div id="map"></div>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script src="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.js"></script>
<script src="https://unpkg.com/@maplibre/maplibre-gl-leaflet@0.1.3/leaflet-maplibre-gl.js"></script>
<script>
const apiKey = 'YOUR_MAPPINEST_KEY';
async function initMap() {
// Fetch TileJSON first, then pass the returned raster tile URL to L.tileLayer.
const response = await fetch(
`https://api.mappinest.com/v1/tiles/mappinest.countries-raster.json?key=${apiKey}`
);
if (!response.ok) {
throw new Error('TileJSON request failed.');
}
const tilejson = await response.json();
const tilesUrl = tilejson.tiles?.[0];
if (!tilesUrl) {
throw new Error('TileJSON did not return a tile URL.');
}
const map = L.map('map').setView([0, 0], 1);
L.maplibreGL({
style: `https://api.mappinest.com/v1/maps/light/style.json?key=${apiKey}`,
interactive: false
}).addTo(map);
// Raster tiles can be added directly with L.tileLayer.
L.tileLayer(tilesUrl, {
maxZoom: tilejson.maxzoom ?? 14,
crossOrigin: 'anonymous',
opacity: 0.9
}).addTo(map);
}
initMap();
</script>
</body>
</html>import { useEffect, useRef } from 'react';
import L from 'leaflet';
import '@maplibre/maplibre-gl-leaflet';
import 'leaflet/dist/leaflet.css';
import 'maplibre-gl/dist/maplibre-gl.css';
const apiKey = 'YOUR_MAPPINEST_KEY';
export default function MappinestLeafletRasterTilesMap() {
const containerRef = useRef<HTMLDivElement | null>(null);
const mapRef = useRef<L.Map | null>(null);
useEffect(() => {
if (!containerRef.current || mapRef.current) return;
let cancelled = false;
async function initMap() {
// Fetch TileJSON first, then pass the returned raster tile URL to L.tileLayer.
const response = await fetch(
`https://api.mappinest.com/v1/tiles/mappinest.countries-raster.json?key=${apiKey}`
);
if (!response.ok) {
throw new Error('TileJSON request failed.');
}
const tilejson = await response.json();
if (cancelled || !containerRef.current) return;
const tilesUrl = tilejson.tiles?.[0];
if (!tilesUrl) {
throw new Error('TileJSON did not return a tile URL.');
}
const map = L.map(containerRef.current).setView([0, 0], 1);
mapRef.current = map;
(L as any).maplibreGL({
style: `https://api.mappinest.com/v1/maps/light/style.json?key=${apiKey}`,
interactive: false
}).addTo(map);
// Raster tiles can be added directly with L.tileLayer.
L.tileLayer(tilesUrl, {
maxZoom: tilejson.maxzoom ?? 14,
crossOrigin: 'anonymous',
opacity: 0.9
}).addTo(map);
}
void initMap();
return () => {
cancelled = true;
mapRef.current?.remove();
mapRef.current = null;
};
}, []);
return <div ref={containerRef} style={{ height: 420, width: '100%' }} />;
}URL patterns used in this example
The bold parameters are the values you replace. Prefer TileJSON as the integration request so the raster tile template and zoom metadata stay behind one URL.
Get your free API key in API Keys & Access, then replace YOUR_KEY in the example.
Common errors
What to read next
Last updated: November 3, 2026