1Money Network
  • Getting Started
  • Overview
  • Developer Guide
  • Quick Start
  • User Guides
    • Custodians & Centralized Exchanges (CEXs)
    • Issuers
  • Validators
  • Integrations
    • Overview
    • Network Access
    • SDKs
      • Typescript
        • Get Started
        • API Methods
      • Golang
        • Get Started
        • API methods
      • Rust
    • REST APIs
      • Chains
        • Get Chain Id
      • Accounts
        • Get account by token
        • Get account nonce
      • Tokens
        • Issue token
        • Get token metadata
        • Update token metadata
        • Grant authority
        • Mint
        • Burn
        • Managelist
        • Pause/Unpause
      • Checkpoints
        • Get the latest Checkpoint number
        • Get checkpoint by number
        • Get checkpoint by hash
      • Transactions
        • Get transaction by hash
        • Get transaction receipt by hash
        • Submit payment
        • Submit cancellation
        • Estimate fee
    • Websocket
      • Subscribing & Unsubscribing
      • Retrieving Transaction Details
      • Keeping the connection alive
      • Stream | Checkpoints
    • Data Dictionary
      • Transaction Types
        • TokenCreate
        • TokenTransfer
        • TokenGrantAuthority
        • TokenRevokeAuthority
        • TokenBlacklistAccount
        • TokenWhitelistAccount
        • TokenMint
        • TokenBurn
        • TokenCloseAccount
        • TokenPause
        • TokenUnpause
        • TokenUpdateMetadata
          • TokenMetadata Structure
            • MetaDataKeyValuePair
        • Other Transaction Types (WiP)
  • Core Concepts
    • The 1Money Protocol
    • System Components
    • Account Model
    • Token Authority
    • Transactions and Instructions
    • Implementation and usability considerations
    • Security
Powered by GitBook
On this page
  • Install
  • Initialize the API Client
  • Fetch the current checkpoint number
  • Get checkpoint by number
  • CDN
  • Error Handling
  • Async/Await
  • Promise
  1. Integrations
  2. SDKs
  3. Typescript

Get Started

Install

npm i -S @1money/ts-sdk axios ethers
# or
yarn add @1money/ts-sdk axios ethers
# or
pnpm i @1money/ts-sdk axios ethers

Initialize the API Client

import { api } from '@1money/ts-sdk';

// Initialize with default settings (mainnet)
const apiClient = api();

// Or specify testnet network
const testnetClient = api({ network: 'testnet' });

// You can also set a custom timeout (in milliseconds)
const apiClient = api({
  network: 'testnet',
  timeout: 5000 // 5 seconds
});

Fetch the current checkpoint number

const number = await apiClient.checkpoints.getNumber()
  .success(response => {
    console.log('number', response.number);
    return response.number;
  })
  .error(err => {
    console.error('Error:', err);
    // return a default value
    return 0;
  });

// do something with the number
// ...

Get checkpoint by number

const checkpoint = await apiClient.checkpoints.getByNumber(1)
  .success(response => {
    console.log('checkpoint', response);
  });

CDN

<script src="https://unpkg.com/@1money/ts-sdk@latest/umd/1money-ts-sdk.min.js"></script>

<script>
  const apiClient = window.$1money.api({
    network: 'testnet'
  });

  async function getNumber () {
    const number = await apiClient.checkpoints.getNumber();
    console.log('number', number);
  }

  getNumber();
</script>

Error Handling

All API methods return a promise-like object with .success(), .timeout(), .error() and .rest() handlers. Always implement both handlers for proper error management:

  1. .success(): Handles successful API responses

  2. .timeout(): Specifically handles timeout errors

  3. .error(): Handles all other types of errors

  4. .rest(): A final handler that runs after any of the above handlers complete

import { api } from '@1money/ts-sdk';

const apiClient = api();

apiClient.someMethod()
  .success(response => {
    // Handle successful response
  })
  .timeout(err => {
    // Handle timeout case
  })
  .error(err => {
    // Handle other errors
  });

You can use rest to handle all other errors:

apiClient.someMethod()
  .success(response => {
    // Handle successful response
  })
  .rest(err => {
    // Handle other cases
  });

Async/Await

You also can use async/await to handle the response:

import { api } from '@1money/ts-sdk';

const apiClient = api();

try {
  const response = await apiClient.someMethod();
  console.log('Response:', response);
} catch (err) {
  console.error('Error:', err);
}

Promise

You also can use standard promise to handle the response:

import { api } from '@1money/ts-sdk';

const apiClient = api();

apiClient.someMethod()
  .then(response => {
    console.log('Response:', response);
  })
  .catch(err => {
    console.error('Error:', err);
  });
PreviousTypescriptNextAPI Methods

Last updated 15 days ago