# edge-impulse-api-test

> Node.js bindings for the Edge Impulse API

Latest version **1.33.5** (published 2023-11-10) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install edge-impulse-api-test
pnpm add edge-impulse-api-test
yarn add edge-impulse-api-test
bun add edge-impulse-api-test
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.33.5 |
| Published | 2023-11-10 |
| First published | 2023-11-01 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 8.4 MB |
| Known vulnerabilities | 0 (+7 in 4 direct dependencies) |
| Install scripts | no |
| Author | EdgeImpulse Inc. |
| Maintainers | janjongboom1 |

## Links

- npm: https://www.npmjs.com/package/edge-impulse-api-test
- Repository: https://github.com/edgeimpulse/api-bindings-node
- Homepage: https://github.com/edgeimpulse/api-bindings-node#readme
- Issues: https://github.com/edgeimpulse/api-bindings-node/issues
- npm.io page: https://npm.io/package/edge-impulse-api-test

## Dependencies (8)

- [ws](https://npm.io/package/ws.md) 7.2.0
- [tsee](https://npm.io/package/tsee.md) 1.3.0
- [request](https://npm.io/package/request.md) 2.88.0
- [form-data](https://npm.io/package/form-data.md) 4.0.0
- [socket.io](https://npm.io/package/socket.io.md) 2.4.1
- [node-fetch](https://npm.io/package/node-fetch.md) 2.6.7
- [typed-emitter](https://npm.io/package/typed-emitter.md) 1.3.1
- [request-promise](https://npm.io/package/request-promise.md) 4.2.4

## Recent versions

- 1.33.5 (latest) — 2023-11-10
- 1.33.3 — 2023-11-08
- 1.25.33 — 2023-11-01

## README

# Node.js bindings to the Edge Impulse Studio API

This package contains bindings to the [Edge Impulse API](https://docs.edgeimpulse.com/reference/edge-impulse-api/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](https://docs.edgeimpulse.com/reference/edge-impulse-api/edge-impulse-api) 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**

```js
const EdgeImpulseApi = require('edge-impulse-api').EdgeImpulseApi;
const api = new EdgeImpulseApi();
```

**TypeScript**

```js
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.

```js
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.

```js
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](https://docs.edgeimpulse.com/reference/edge-impulse-api/projects/list-active-projects) and [Project information](https://docs.edgeimpulse.com/reference/edge-impulse-api/projects/project-information).

```js
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:

```js
(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);
    }
})();
```

---
_Source: https://npm.io/package/edge-impulse-api-test · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
