edge-impulse-api
v1.61.7
Published
Node.js bindings for the Edge Impulse API
Downloads
1,994
Readme
Node.js bindings to the Edge Impulse Studio API
This package contains bindings to the Edge Impulse API - it lets you automate anything that you can do through the Studio UI (and much more).
We're currently working on updating the API docs to reference this library in the examples. Until then we recommend you use the TypeScript types to see the exact specs for calling the API functions.
Installing this package
$ npm install edge-impulse-api
Examples
Instantiating this library
JavaScript
const EdgeImpulseApi = require('edge-impulse-api').EdgeImpulseApi;
const api = new EdgeImpulseApi();
TypeScript
import { EdgeImpulseApi } from 'edge-impulse-api';
const api = new EdgeImpulseApi();
Authenticating
API Key
API Keys give access to a single project, and have a permission scope.
const EdgeImpulseApi = require('edge-impulse-api').EdgeImpulseApi;
(async () => {
const api = new EdgeImpulseApi();
await api.authenticate({
method: 'apiKey',
apiKey: 'ei_...', // your API key here
});
})();
Username / password
Username / password authentication gives access to every single project in your account. Where possible use API keys instead (which also have permission scoping). You can cache the JWT token (typically valid for 30 days, unless your enterprise administrator has reconfigured this) to avoid putting your username/password into code.
const EdgeImpulseApi = require('edge-impulse-api').EdgeImpulseApi;
(async () => {
try {
const api = new EdgeImpulseApi();
const jwtToken = await api.login.login({
username: 'myusername',
password: 'mypassword'
});
await api.authenticate({
method: 'jwtToken',
jwtToken: jwtToken.token || '',
});
}
catch (ex) {
// check token expiration via:
const tokenExpired = (ex.message || ex.toString()).indexOf('Your JWT token has expired') > -1;
if (tokenExpired) {
console.log('Token was expired, need to re-log in', ex);
process.exit(1);
}
console.log('Failed to make a request', ex);
process.exit(1);
}
})();
Calling API functions
Here's an example of List active projects and Project information.
let projects = await api.projects.listProjects();
console.log('all projects', projects.projects.map(p => {
return {
id: p.id,
name: p.name,
owner: p.owner,
};
}));
let projectInfo = await api.projects.getProjectInfo(projects.projects[0].id);
console.log('projectInfo', projectInfo);
As stated above, the TypeScript type hints are a good way to discover the request and response types.
Running jobs
Long-running API calls (e.g. training a network) return a job. Here's how you wait for a job to complete, and how to get job log messages:
(async () => {
try {
const PROJECT_ID = 12345
let impulseRes = await api.impulse.getImpulse(PROJECT_ID);
if (!impulseRes.impulse) {
throw new Error('Project has no impulse');
}
let kerasBlock = impulseRes.impulse.learnBlocks.find(x => x.type.indexOf('keras') > -1);
if (!kerasBlock) {
throw new Error('Failed to find a Keras block in ' + JSON.stringify(impulseRes.impulse, null, 4));
}
// grab the config
let kerasConfig = await api.learn.getKeras(PROJECT_ID, kerasBlock.id);
// and retrain with same config
let trainJob = await api.jobs.trainKerasJob(PROJECT_ID, kerasBlock.id, kerasConfig);
console.log('Created train job with ID', trainJob.id);
await api.runJobUntilCompletion({
type: 'project',
projectId: PROJECT_ID,
jobId: trainJob.id,
}, data => {
process.stdout.write(data);
});
console.log('Train job completed');
}
catch (ex) {
console.log('Failed to make a request', ex);
process.exit(1);
}
})();