OpenStreetMap API for JavaScript/TypeScript

4.0.0 · active · verified Wed Apr 22

The `osm-api` package provides a robust JavaScript/TypeScript wrapper for interacting with the OpenStreetMap API, designed for both Node.js (requiring Node.js >=18) and browser environments. Currently stable at version 4.0.0 (last published February 2026), the library facilitates common OSM operations such as fetching map features, managing changesets, user data, messaging, and notes. It automatically converts OSM's XML responses into JSON format, simplifying data handling for developers. A key differentiator is its use of OAuth 2 for authentication, enhancing security by avoiding the direct exposure of OAuth `client_secret`s. The library is lightweight (24 kB gzipped) and offers a simpler API and TypeScript support compared to older alternatives like `osm-request`. While a specific fixed release cadence is not explicitly stated, updates are released to address critical changes, such as the security patches to OSM's authentication flow, requiring timely library updates (e.g., v3.0.0 for a specific popup mode authentication fix).

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates fetching a public OSM feature and outlines the pattern for authenticated API calls using OAuth 2, including necessary placeholders.

import * as OSM from 'osm-api';

async function runOsmApiExample() {
  try {
    // Public API call - no authentication required
    const feature = await OSM.getFeature('way', 23906749);
    console.log('Fetched feature:', feature);

    // Authenticated API calls require login first.
    // In a real application, replace these with your actual OAuth 2 client details
    // and handle the authentication flow (e.g., popup or redirect).
    // This is a placeholder for demonstration purposes.
    const authConfig = {
      client_id: process.env.OSM_CLIENT_ID ?? 'YOUR_CLIENT_ID',
      redirect_uri: process.env.OSM_REDIRECT_URI ?? 'YOUR_REDIRECT_URI',
      scope: 'write_changesets,write_notes',
    };
    
    // This would typically involve a browser redirect or popup.
    // For a Node.js script, you'd likely manage tokens manually after an initial authorization step.
    // const authenticatedOSM = await OSM.login(authConfig);
    // console.log('Successfully attempted login (mock):', authenticatedOSM);

    // Example of an authenticated call (requires actual login to work)
    // Assuming an authenticated instance or global auth state after login
    // For this example, we'll simulate the call, but it won't work without actual auth.
    console.log('Attempting to create a changeset comment (requires authentication)...');
    // const commentResult = await OSM.createChangesetComment(114733070, 'Thanks for your edit!');
    // console.log('Changeset comment result:', commentResult);

  } catch (error) {
    console.error('An error occurred:', error.message);
    if (error.response) {
      console.error('OSM API Error details:', error.response.status, error.response.data);
    }
  }
}

runOsmApiExample();

view raw JSON →