{"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+.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mg-api-js"],"cli":null},"imports":["const GeotabApi = require('mg-api-js');","const api = new window.GeotabApi(authentication, options);","import GeotabApi from 'mg-api-js';"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}