SDK Usage

How to use the SDK


npm install -g mem-sdk

Import MEM SDK

React, and other frameworks (also check out React Version):

import Mem from "mem-sdk";

const mem : Mem = new Mem({
  network: "mainnet" || "testnet"

Node (TS)

const { Mem } = require("mem-sdk");
const mem: Mem = new Mem({
  network: "mainnet" || "testnet"

Node (ES6)

import { Mem } from "mem-sdk"
const mem = new Mem({
  network: "mainnet" || "testnet"

Retrieve a function state

const FUNCTION_ID = "...";
const state = await;

Send an interaction (only works on server-side)

const FUNCTION_ID = "...";
const inputs = { function: "test", inputA: "valueA" };

const txid = await mem.write(FUNCTION_ID, inputs);

Deploy a function

const SRC = `export async function handle(state, action) {
  const input = action.input;

if (input.function === "save") {
    const { username, bio } = input;

    ContractAssert(username.trim().length, "ERROR_INVALID_INPUT");
    ContractAssert(typeof username === "string" && typeof bio === "string");
    state.logs.push ({ username, bio });
    return { state }

const INIT_STATE = '{"logs": []}';

const id = await mem.deploy(SRC, INIT_STATE);

Fork a function to mainnet or testnet

While the SDK should be initialized for mainnet using this method, the fork functionality allows forking mainnet deployed function to either mainnet or carbon testnet:

const MAINNET_FUNCTION_ID = "...";
const FORK_IT_TO = "mainnet" || "testnet";
const OVERWRITE_INIT_STATE = btoa('{"new": "state"}'); // optional


Get a function's KV object (only mainnet)

const MAINNET_FUNCTION_ID = "...";
const kv = await mem.kvGet(MAINNET_FUNCTION_ID);

Named functions resolving DX (only mainnet)

It is possible to assign a memorable name to a function ID using the function registry tool (e.g., ans.mem instead of Tih...I5M). These .mem names are resolvable with the MEM SDK the same way you would pass a function ID:


Source code

The MEM SDK is an OSS licensed under the MIT license, check out the code's repository here.

Last updated ยฉ 2023