{"id":13538,"library":"mg-api-js","title":"MyGeotab API JavaScript Client","description":"The `mg-api-js` library provides a robust JavaScript wrapper for interacting with the MyGeotab API, designed for both client-side browser environments and Node.js applications. Currently stable at version `3.0.2`, the library offers a convenient abstraction over direct API calls, managing authentication via username/password or session IDs. It includes built-in credential storage (localStorage in browsers, a mock in Node.js) with options for custom datastore integration. Key differentiators include its dual-environment support and direct integration with MyGeotab's authentication mechanisms, simplifying common API interaction patterns. Releases occur on an as-needed basis, addressing bugs, security patches, and Node.js compatibility updates, such as recent fixes for Node.js 22+.","status":"active","version":"3.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/Geotab/mg-api-js","tags":["javascript"],"install":[{"cmd":"npm install mg-api-js","lang":"bash","label":"npm"},{"cmd":"yarn add mg-api-js","lang":"bash","label":"yarn"},{"cmd":"pnpm add mg-api-js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package's documentation primarily shows CommonJS `require()` for Node.js environments. Attempting to use named ESM imports for `GeotabApi` will fail as it exports as a CommonJS default.","wrong":"import { GeotabApi } from 'mg-api-js';","symbol":"GeotabApi","correct":"const GeotabApi = require('mg-api-js');"},{"note":"For browser environments, the library is loaded via a script tag (e.g., CDN) and exposes `GeotabApi` as a global variable on the `window` object.","wrong":"import GeotabApi from 'https://cdn.jsdelivr.net/npm/mg-api-js';","symbol":"GeotabApi","correct":"const api = new window.GeotabApi(authentication, options);"},{"note":"While CommonJS `require()` is shown in the documentation, modern Node.js environments (v18+) support `import GeotabApi from 'mg-api-js'` in ESM modules due to CommonJS interop for default exports. Ensure your project is configured for ESM if using this syntax.","wrong":"const GeotabApi = require('mg-api-js'); // In a native ESM module","symbol":"GeotabApi (ESM Interop)","correct":"import GeotabApi from 'mg-api-js';"}],"quickstart":{"code":"const GeotabApi = require('mg-api-js');\n\n// Replace with your actual Geotab credentials and server path\nconst authentication = {\n    credentials: {\n        database: process.env.GEOTAB_DATABASE ?? 'your_database_name',\n        userName: process.env.GEOTAB_USERNAME ?? 'your_username',\n        password: process.env.GEOTAB_PASSWORD ?? 'your_password'\n    },\n    // If not using a sessionId, 'path' can be omitted to default to 'my.geotab.com'\n    // If using sessionId, 'path' is required.\n    path: process.env.GEOTAB_SERVER_PATH ?? 'your.server.address' \n};\n\nconst options = {\n    rememberMe: true, // Store credentials/session in datastore (localStorage or mock)\n    timeout: 30,      // API call timeout in seconds\n    // newCredentialStore: new YourCustomDataStore() // Uncomment to provide your own datastore\n};\n\n// Create an instance of the GeotabApi wrapper\nconst api = new GeotabApi(authentication, options);\n\n// Example: Authenticate and then make a simple API call\nasync function runApiCall() {\n    try {\n        console.log('Attempting to authenticate with MyGeotab API...');\n        \n        // This is a placeholder for a common Geotab API call.\n        // Replace 'Get' and the parameters with an actual method and parameters you need.\n        const result = await api.call('Get', {\n            typeName: 'Device',\n            search: {\n                name: 'YourTestDevice' \n            },\n            resultsLimit: 1\n        });\n\n        console.log('API Call successful. Devices found:', result);\n        if (result && result.length > 0) {\n            console.log('First device name:', result[0].name);\n        }\n\n    } catch (error) {\n        console.error('An error occurred during API interaction:', error);\n        if (error.response) {\n            console.error('HTTP Status:', error.response.status);\n            console.error('Response Data:', error.response.data);\n        }\n    } finally {\n        console.log('API interaction attempt finished.');\n    }\n}\n\nrunApiCall();","lang":"javascript","description":"Demonstrates how to initialize the GeotabApi client with credentials and options, then make a basic API call to retrieve device information. Uses environment variables for sensitive data."},"warnings":[{"fix":"Review the updated documentation on 'Full Responses' in the README to adapt your code to the new response structure when `fullResponse` is set to true.","message":"The `fullResponse` option no longer provides the full Axios response object directly since v3.0.0. The structure of the response when `fullResponse` is enabled has changed.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure your Node.js environment is updated to version 18.0.0 or higher to use `mg-api-js` v3.0.2 and above.","message":"The minimum required Node.js version has been increased to 18.0.0.","severity":"breaking","affected_versions":">=3.0.2"},{"fix":"Remove the authentication callback from the `GeotabApi` constructor. Authentication is now handled implicitly or through direct credential/session ID objects.","message":"The `GeotabApi` object no longer accepts an authentication callback function as a parameter during instantiation.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Always provide the full server address in the `path` field of the authentication object if you are using a `sessionId`.","message":"When authenticating with a `sessionId`, the `path` property in the authentication object is a required parameter.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Explicitly set the `path` property in the authentication object if your Geotab server is not `my.geotab.com` to avoid incorrect routing.","message":"If providing a username/password for initial authentication and omitting the `path` property, the library will default the server address to `my.geotab.com`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"To override the default storage, provide an object implementing `get()`, `set()`, and `clear()` methods to the `newCredentialStore` option during API initialization.","message":"By default, credentials/sessions are stored in `localStorage` in browsers and a `LocalStorageMock` in Node.js. If you use a custom datastore, it must implement `get()`, `set()`, and `clear()` methods.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade to `mg-api-js` version `2.1.3` or higher to ensure you have the security patches for `follow-redirects`.","message":"A security vulnerability related to the `follow-redirects` dependency was patched in `v2.1.3`.","severity":"gotcha","affected_versions":"<2.1.3"}],"env_vars":null,"search_vec":"'22':118 '3.0.2':37 'abstract':43 'address':104 'api':2,8,21,46,93 'applic':32 'as-need':100 'authent':49,89 'basi':103 'browser':28,64 'bug':105 'built':58 'built-in':57 'call':47 'client':4,26 'client-sid':25 'common':92 'compat':110 'conveni':42 'credenti':60 'current':33 'custom':72 'datastor':73 'design':22 'differenti':76 'direct':45,84 'dual':80 'dual-environ':79 'environ':29,81 'fix':115 'id':54 'includ':56,77 'integr':74,85 'interact':17,94 'javascript':3,14,119 'js':9 'key':75 'librari':10,39 'localstorag':62 'manag':48 'mechan':90 'mg':7 'mg-api-j':6 'mock':66 'mygeotab':1,20,87 'need':102 'node.js':31,68,109,117 'occur':97 'offer':40 'option':70 'patch':107 'pattern':95 'provid':11 'recent':114 'releas':96 'robust':13 'secur':106 'session':53 'side':27 'simplifi':91 'stabl':34 'storag':61 'support':82 'updat':111 'username/password':51 'version':36 'via':50 'wrapper':15","created_at":"2026-04-20T01:55:01.134260+00:00","updated_at":"2026-04-20T01:55:01.134260+00:00","problems":[{"fix":"Upgrade to `mg-api-js` version `3.0.2` or higher to fix compatibility with Node.js 22+.","cause":"Running `mg-api-js` in Node.js 22+ environments before v3.0.2, where a fix for localStorage detection was implemented.","error":"ReferenceError: localStorage is not defined"},{"fix":"Use `import GeotabApi from 'mg-api-js';` for ESM environments, or `const GeotabApi = require('mg-api-js');` for CommonJS environments.","cause":"Attempting to use `import { GeotabApi } from 'mg-api-js'` in a Node.js ESM module when `mg-api-js` primarily exposes its API as a CommonJS default export.","error":"TypeError: GeotabApi is not a constructor"},{"fix":"Review the 'Full Responses' section in the library's README for `v3.0.0` onwards and adjust your code to handle the new response object structure.","cause":"Code written for `mg-api-js` versions prior to `v3.0.0` expecting the `fullResponse` option to return the complete Axios response object, which was altered in `v3.0.0`.","error":"Error: Response object structure changed for fullResponse option"},{"fix":"Upgrade to `mg-api-js` version `2.0.1` or higher to resolve the missing `api.min.js` file for Node.js.","cause":"A packaging error in earlier versions (`v2.0.0`) that prevented `api.min.js` from being correctly bundled for Node.js usage.","error":"TypeError: api.min.js missing for nodejs"},{"fix":"Upgrade to `mg-api-js` version `2.1.1` or higher to fix the bug with empty errors on multi-calls.","cause":"A bug in earlier versions (`v2.1.0`) causing multi-calls to return an empty error object.","error":"Error: Empty error when making multi-call"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":"https://my.geotab.com","github":"https://github.com/Geotab/mg-api-js","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/mg-api-js","openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","auth-security","web-framework"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}