There are SDKs available for working with jsonpad.io. Right now we've got a Javascript SDK, which you can use in Node.JS applications or in the browser, and a Realtime SDK which makes it easy to work with realtime updates.
Here's an example of how you can use the JavaScript/TypeScript SDK in a Node.js application.
First, install the jsonpad.io SDK NPM package:
npm install @basementuniverse/jsonpad-sdk
Next, here's how to fetch a page of items from a list with the path name "my-list":
const JSONPad = require('@basementuniverse/jsonpad-sdk').default;
const jsonpad = new JSONPad('<YOUR TOKEN>');
jsonpad.fetchItems('my-list', {
page: 1,
limit: 10,
order: 'createdAt',
direction: 'desc',
}).then(response => {
// For now we'll just log the data of each item
console.log(response.data.map(item => item.data));
});Check out the full SDK documentation here.
Let's try handling a realtime event when an item is added to the list.
First, install the jsonpad.io Realtime SDK NPM package:
npm install @basementuniverse/jsonpad-realtime-sdk
Then, we can start listening for the item-created event on our list. Note that we need to use the list id instead of the path name.
const JSONPadRealtime = require('@basementuniverse/jsonpad-realtime-sdk').default;
const jsonpadRealtime = new JSONPadRealtime('<YOUR TOKEN>');
jsonpadRealtime.listen(
[
'item-created',
],
[
'0d04819b-1c08-4557-b2e4-b6e56a54d94c',
]
);
jsonpadRealtime.addEventListener('item-created', e => {
console.log(
`An item with id ${e.detail.model.id} was created!`
);
});Check out the Realtime SDK documentation here.
Let's try the same thing as above (fetching a list and listening for items being created in realtime), but this time we'll do it entirely in the browser:
<script src="https://cdn.jsdelivr.net/npm/@basementuniverse/jsonpad-sdk@1.0.0/build/jsonpad-sdk.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@basementuniverse/jsonpad-realtime-sdk@1.0.0/build/jsonpad-realtime-sdk.js"></script>
<script>
const jsonpad = new JSONPad.default('<YOUR TOKEN>');
jsonpad.fetchList('test').then(list => {
console.log(list);
});
const jsonpadRealtime = new JSONPadRealtime.default('<YOUR TOKEN>');
jsonpadRealtime.listen(
[
'item-created',
],
[
'0d04819b-1c08-4557-b2e4-b6e56a54d94c',
]
);
jsonpadRealtime.addEventListener('item-created', e => {
console.log(
`An item with id ${e.detail.model.id} was created!`
);
});
</script>Some things to note:
view permissions.