The @hoprnet/hopr-sdk
package is a software development kit for interacting with HOPRd's Rest API functions.
It provides a set of functions that allow developers to interact with the HOPR protocol and perform various actions such as node and account management, messaging, address retrieval, balance retrieval, and withdrawal and other operations.
To install the @hoprnet/hopr-sdk
package, follow these steps:
npm install @hoprnet/hopr-sdk
or
yarn add @hoprnet/hopr-sdk
You can use the @hoprnet/hopr-sdk
in two different ways:
By creating a new instance of the HoprSDK class.
import { HoprSDK } from '@hoprnet/hopr-sdk';
const sdk = new HoprSDK({
apiEndpoint: 'http://localhost:3001', // Replace with your HOPR API endopoint.
apiToken: 'your-api-token', // Replace with your API token
timeout: 5000 // Optional timeout in milliseconds (defaults to 30000)
});
sdk
instance to access the available functions. For example, to get the HOPR and native addresses associated with the node:const addresses = await sdk.api.account.getAddresses();
console.log(addresses);
By calling directly the rest API functions.
import { api } from '@hoprnet/hopr-sdk';
"moduleResolution": "nodenext" or "node16"
, you can import the functions like this:import { getAddresses } from '@hoprnet/hopr-sdk/api';
apiEndpoint
and apiToken
for each individual function:const addresses = await api.getAddresses({
apiEndpoint: 'http://localhost:3001', // Replace with your HOPR API endopoint
apiToken: 'your-api-token' // Replace with your API token
timeout: 5000 // Optional timeout in milliseconds (defaults to 30000)
});
console.log(addresses);
By calling directly the flows functions.
import { flows } from '@hoprnet/hopr-sdk';
"moduleResolution": "nodenext" or "node16"
, you can import the functions like this:import { openMultipleChannels } from '@hoprnet/hopr-sdk/flows';
apiEndpoint
and apiToken
for each individual function:const res = await flows.openMultipleChannels({
apiEndpoint: 'http://localhost:3001', // Replace with your HOPR API endopoint
apiToken: 'your-api-token' // Replace with your API token
timeout: 60e3 * 7 // Optional timeout in milliseconds (defaults to 30000) | This function takes really long
});
console.log(res);
Source code resides under src/
folder.
fetchWithTimeout
The api folder is used to find routes with ease.
For example if you are looking for GET /api/v3/node/info
you would go straight
to the node folder and find the file getInfo.ts
alongside getInfo.spec.ts
to
understand how this function will react to different responses from the node.
In a more general sense it looks like this:
├── api
│ ├── resource (example: node)
│ │ ├── adapter.ts
│ │ ├── getResource.ts
│ │ ├── getResource.spec.ts
The adapter creates a resource class used by the sdk class
The resource of the route can be seen after the v3
keyword, for example in this example /api/v3/node/info
the resource is node
.
async function getResource(payload: ResourcePayloadType) {
const rawResponse = await fetchResource()
// received unexpected error from server
if (isServerError(rawResponse)) {
throw new Error();
}
// received expected response
// this can either be done with a Zod object or a status code
if (receivedExpectedResponse(rawResponse)) {
return parseResponse(rawResponse);
}
// check if response has the structure of an expected sdk api error
if (isApiErrorResponse(rawResponse)) {
throw new sdkApiError();
}
// if we get to this point then we could not parse the response
// and it is not unexpected error
// we probably need to update the zod type
throw new ZodError(parsedRes.error.issues);
}
To contribute to this repository you will need to create a pull request. More information about the existing automated workflows can be found in GitHub Actions