Quickstart
The Watch SDK allows you to interact with Analog Watch in standard Node.js or Web environments.
Prerequisites
Before using the Watch SDK, ensure the following are set up:
- Node.js: Install the latest version.
- Package Manager: Use npm or yarn.
- Substrate-based Wallet: Required for Analog Watch compatibility. For supported options, see Supported Wallets.
- Node.js installed (use the latest version).
- Smart Contract Address: Browse available contracts on the Analog Watch Testnet or refer to Listing Smart Contracts for setup details.
Installation
Depending on your choice of package manager, you can install the SDK in two ways:
npm
npm i @analog-labs/timegraph-jsAuthentication
The Watch SDK supports both web and Node.js environments.
- Web: Requires the key generation tool (provided by
timegraph.js) and the@polkadot/extension-dapplibrary. - Node: Requires the key generation tool and the
@polkadot/keyringpackage.
Authentication follows a three-step process:
1. Install extensions and initialize them
Depending on your preferred environment, proceed to generate the SSK as follows:
Web
Install @polkadot/extension-dapp as follows:
npm i @polkadot/extension-dapp //npm installation for @polkadot/extension-dapp
yarn add @polkadot/extension-dapp //yarn installation for @polkadot/extension-dappImport the keygen and web3FromAdress and proceed to generate the session keys.
import { keygen } from '@analog-labs/timegraph-js';
import { web3FromAddress } from '@polkadot/extension-dapp';
// init signer based on polkadot/extension-dapp
const { signer } = await web3FromAddress( '<Wallet Address>' );
// pass signer and address to get keygen instance
const _keygen = new keygen({ signer: signer.signRaw,address: '<Wallet Address>'});
// generate api key i.e, role is optional default will be developer
const apikey = await _keygen.createApiKey();
//Generate the session key
const sessionKey = await _keygen.createSessionkey();Response
"apiKey":{
"key":"HtACKxQe9dx4Xmg8d1sSd7PQnzvNKSS9ViBb2Aq8Fdo",
"secret":"9yPmWeRLcpGn9rAeqg8npRyBmgA9D8D94KPTpfkD8Kgd",
"cert":"111CfKkxwN6abhKrnZ4wKEWdpagvfNqe6iUR5Gg....."
},
{
"ssk":"0;EFVS6JNJ7ah615F69cTHsDrUP2vziy1943hyW6LUbkfr;0;*;1;26528g8cdQfzBw2ue85Hbkcw3cpRmrfrhjhpEScxeKhQuVMYCKzoucNcXUmXEiypg9G7188n9h2pzLCRwD1BFFk3"
}Instead of using the keygen and @polkadot/extension-dapp/ @polkadot/Keyring packages, you can generate the session keys via an inbuilt Session Key Generator (SKG) toolkit.
2. Create an account
With the session keys generated, you can now create an account by providing the wallet address and SSK to the SDK:
import { TimegraphClient } from '@analog-labs/timegraph-js';
const timeGraphClient = new TimegraphClient({
url: '<URL>', // url to Watch GraphQL instance
sessionKey: '<SSK>', // session key created by user wallet using SDK keygen
});
const response = timeGraphClient.user.create({ address: '<Wallet Address>' }); Response
"response":{
{
"userId":111,
"wallet":"5GWqFcdT8ugZW8fZhZHQzcx96M2jQYLV7M9KMozYPzLfEdR9",
"userRole":"developer",
"subgraph":111
}
}3. Validate the certificate data
To validate the certificate data for a new API key and save it in storage, you must pass a valid SSK for the signing account as follows:
import { TimegraphClient } from '@analog-labs/timegraph-js';
const timeGraphClient = new TimegraphClient({
url: '<URL>', // url to Watch GraphQL instance
sessionKey: '<SSK>', // session key created by user wallet using SDK Keygen
});
const response = timeGraphClient.apiKey.certify({ cert: '<certificate>' }); Response
"response":{
"certifyKey":{
"userId":100"cert":"111CfKkxwN6abhKrnZ4wKEWdpagvfNqe6iUR5Gg.....",
"key":"HtACKxQe9dx4Xmg8d1sSd7PQnzvNKSS9ViBb2Aq8Fdo",
"role":"developer",
"status":"active"
}
}Note:
You can also enable the previously revoked API key by passing a valid session key for the key's owner like this:
import { TimegraphClient } from '@analog-labs/timegraph-js';
const timeGraphClient = new TimegraphClient({
url: '<URL>', // url to Watch GraphQL instance
sessionKey: '<SSK>', // session key created by user wallet using SDK keygen
});
const response = timeGraphClient.apiKey.enable({ key: '<Api key>' }); Likewise, you can also revoke an API key by passing a valid session key for the key's owner in the Authorization header:
import { TimegraphClient } from '@analog-labs/timegraph-js';
const timeGraphClient = new TimegraphClient({
url: '<URL>', // url to Watch GraphQL instance
sessionKey: '<SSK>', // session key created by user wallet using SDK Keygen
});
const response = timeGraphClient.apiKey.revoke({ key: '<Api key>' }); Initialization
Instantiate and configure the SDK client as follows:
import { TimegraphClient } from '@analog-labs/timegraph-js';
const timeGraphClient = new TimegraphClient({
url: '<https://timegraph.testnet.analog.one/graphql>', // url to Watch GraphQL instance
sessionKey: '<SSK>', // session key created by user wallet using WASM SDK
});Now that you have the two clients, it’s time to start publishing and querying data in a few simple steps, as highlighted below:
Next Steps
Get an in-depth, hands-on experience by deploying and querying Analog Watch artifacts here: