Skip to content

Token-Layer/sdk-typescript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@token-layer/sdk-typescript

TypeScript SDK for POST /token-layer and POST /info.

Also includes a beta WebSocket helper for tokenActivity streams.

Install

npm install @token-layer/sdk-typescript viem

Quick start (wallet auth)

import { TokenLayer } from "@token-layer/sdk-typescript";
import { createWalletClient, custom } from "viem";

const walletClient = createWalletClient({
  chain: undefined,
  transport: custom(window.ethereum),
});

const tokenLayer = new TokenLayer({
  baseUrl: "https://api.tokenlayer.network/functions/v1",
  source: "Mainnet",
  defaults: {
    builder: {
      code: "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
      fee: 50,
    },
  },
  auth: {
    type: "wallet",
    walletClient,
    signatureChainId: "0x1",
  },
});

await tokenLayer.action.register();

const action = tokenLayer.prepare.createToken({
  name: "My Cool Token",
  symbol: "MCT",
  description: "A new token",
  image: "https://example.com/logo.png",
  chainSlug: "base",
});

const createResponse = await tokenLayer.action.createToken({
  action,
});

console.log(createResponse.success);

JWT/API key auth

import { TokenLayer } from "@token-layer/sdk-typescript";

const tokenLayerJwt = new TokenLayer({
  baseUrl: "https://api.tokenlayer.network/functions/v1",
  source: "Mainnet",
  auth: { type: "jwt", token: process.env.JWT! },
});

await tokenLayerJwt.action.createToken({
  action: tokenLayerJwt.prepare.createToken({
    name: "My Cool Token",
    symbol: "MCT",
    description: "A new token",
    image: "https://example.com/logo.png",
    chainSlug: "base",
  }),
});

Auth switching helpers

const base = new TokenLayer({ baseUrl: "https://api.tokenlayer.network/functions/v1" });

const walletTl = base.asWallet(walletClient, { signatureChainId: "0x1" });
const jwtTl = base.asJwt(jwt);
const apiKeyTl = base.asApiKey(apiKey);

API

  • action.register(params?) (wallet auth only)
  • action.createToken(params)
  • action.tradeToken(params) (JWT/API key auth)
  • action.sendTransaction(params) (JWT/API key auth)
  • action.transferToken(params) (JWT/API key auth)
  • action.claimRewards(params) (JWT/API key auth)
  • action.createReferralCode(params) (JWT/API key auth)
  • action.enterReferralCode(params) (JWT/API key auth)
  • action.mintUsd(params) (JWT/API key auth, testnet-only action)
  • prepare.createToken(actionDraft) (local typed helper)
  • info.getTokensV2(params) (no auth required)
  • info.quoteToken(params) (no auth required)
  • info.me(params?) (JWT/API key auth)
  • info.getPoolData(params) (no auth required)
  • info.getUserBalance(params) (JWT/API key auth)
  • info.searchToken(params) (no auth required)
  • info.checkTokenOwnership(params) (no auth required)
  • info.getUserFees(params?) (JWT/API key auth)
  • info.getUserFeeHistory(params?) (JWT/API key auth)
  • info.getLeaderboard(params?) (no auth required)
  • info.getUserPortfolio(params?) (JWT/API key auth)
  • websocket(options?)
  • asWallet(walletClient, opts?)
  • asJwt(token)
  • asApiKey(token)
  • signRegisterRequest(walletClient, params)
  • signCreateTokenRequest(walletClient, params)
  • buildRegisterMessage(address, nonceMs)

Client defaults

Set builder defaults once on client init:

const tokenLayer = new TokenLayer({
  baseUrl: "https://api.tokenlayer.network/functions/v1",
  defaults: {
    builder: { code: "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb", fee: 50 },
  },
});

Merge behavior:

  • action.createToken: if action.builder is missing, SDK injects defaults { code, fee }.
  • info.getTokensV2: if builder_code is missing, SDK injects default builder.code.
  • Explicit per-call values always win over defaults.
  • If defaults.builder.fee is omitted, SDK uses 0.

Info endpoint

import { TokenLayer } from "@token-layer/sdk-typescript";

const tokenLayer = new TokenLayer({
  baseUrl: "https://api.tokenlayer.network/functions/v1",
});

const result = await tokenLayer.info.getTokensV2({
  keyword: "base",
  limit: 20,
  offset: 0,
  order_by: "volume_24h",
  order_direction: "DESC",
});

const quote = await tokenLayer.info.quoteToken({
  chainSlug: "base",
  tokenId: "550e8400-e29b-41d4-a716-446655440000",
  direction: "buy",
  inputToken: "usdc",
  amount: 10,
});

const leaderboard = await tokenLayer.info.getLeaderboard({
  limit: 10,
  offset: 0,
  network_mode: "both",
});

New Action Examples

const authed = tokenLayer.asJwt(process.env.JWT!);

await authed.action.tradeToken({
  action: {
    tokenId: "550e8400-e29b-41d4-a716-446655440000",
    chainSlug: "base",
    direction: "buy",
    buyAmountUSD: 5,
  },
});

await authed.action.sendTransaction({
  action: {
    to: "0x000000000000000000000000000000000000dEaD",
    amount: "1",
    chainSlug: "base",
  },
});

Errors

Requests throw TokenLayerApiError with:

  • status
  • code
  • details
  • message

WebSocket beta

import { TokenLayer } from "@token-layer/sdk-typescript";

const tokenLayer = new TokenLayer({
  baseUrl: "https://api.tokenlayer.network/functions/v1",
  auth: { type: "apiKey", token: process.env.TL_API_KEY! },
});

const ws = tokenLayer.websocket({
  url: "ws://127.0.0.1:8080/ws",
});

await ws.connect();

await ws.subscribeTokenActivity({
  tokenIds: ["your-token-layer-id"],
  chains: ["base-sepolia"],
  activityTypes: ["trade"],
  activitySubtypes: ["buy", "sell"],
  cursor: "latest",
  onEvent(event) {
    console.log(event.seq, event.data.activity_type);
  },
});

Current WebSocket helper support:

  • auth via API key or JWT
  • tokenActivity subscription
  • walletBalances subscription
  • walletFees subscription
  • optional filters for tokenIds, chains, activityTypes, and activitySubtypes
  • optional wallet balance filters for wallet, wallets, tokenId, tokenIds, and chains
  • optional wallet fee filters for wallet, wallets, currency, currencies, activityIds, distributionTypes, and chains
  • event-stream cursors via cursor, sinceTimestamp, and batchSize
  • automatic reconnect and resubscribe with resume from the last seen seq

Wallet-authenticated websocket sessions are not implemented yet.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors