MyGeotab Node.js API Client
This package, `mg-api-node`, functions as an unofficial Node.js client for programmatically interacting with the MyGeotab API. It offers capabilities for authentication, executing API calls (such as `Get`, `call`, and `multicall`), and managing request timeouts and session IDs to optimize repeated interactions. The latest stable version is 1.3.1. Its release cadence has been infrequent, with the most recent feature addition (request timeout functionality) in v1.3.0. While it once served as a direct Node.js interface for MyGeotab, this project is now officially deprecated. Users are strongly advised to transition to the `mg-api-js` package for ongoing support, new feature development, and improved maintainability.
Common errors
-
TypeError: API is not a constructor
cause Attempting to use `mg-api-node` with ES module `import` syntax.fixChange your import statement to `const API = require('mg-api-node');` to correctly load the CommonJS module. -
Error: InvalidUserException
cause Authentication failed due to incorrect username, password, or database name, or insufficient user permissions.fixDouble-check your `userName`, `password`, and `database` parameters. Ensure the provided user has necessary API access within the MyGeotab system. -
Error: read ECONNRESET
cause The connection to the MyGeotab API server was reset, possibly due to server-side issues, network instability, or an intermediate proxy.fixImplement robust error handling and retry mechanisms. Verify network connectivity and check MyGeotab's status page for outages. Ensure your environment's proxy settings are correct if applicable.
Warnings
- breaking This project has been officially deprecated. No further development, bug fixes, or security updates will be provided for `mg-api-node`.
- gotcha The API exclusively uses Node.js callback patterns for asynchronous operations, which can lead to 'callback hell' in complex scenarios.
- gotcha Authentication failures, such as `InvalidUserException`, are common if credentials or the database name are incorrect or user permissions are insufficient.
- gotcha By default, HTTP requests have no explicit timeout, potentially causing long hangs on slow network connections or unresponsive API endpoints.
Install
-
npm install mg-api-node -
yarn add mg-api-node -
pnpm add mg-api-node
Imports
- API
import API from 'mg-api-node';
const API = require('mg-api-node'); - API constructor
const api = mg_api_node(userName, password, database);
const api = new API(userName, password, database);
Quickstart
const API = require('mg-api-node');
const userName = process.env.GEOTAB_USERNAME ?? 'YOUR_USERNAME';
const password = process.env.GEOTAB_PASSWORD ?? 'YOUR_PASSWORD';
const database = process.env.GEOTAB_DATABASE ?? 'YOUR_DATABASE'; // e.g., 'geotab-live'
const api = new API(userName, password, database);
api.authenticate(function(err, data) {
if (err) {
console.error('Authentication Error:', err);
return;
}
console.log('Authenticated successfully. User:', data.userName);
api.call('Get', {
typeName: 'User',
search: {
name: data.userName
}
}, function(err, userData) {
if (err) {
console.error('API Call Error:', err);
return;
}
console.log('User Data:', userData);
});
});