---
# System prepended metadata

title: SDK
tags: [FVM SDK + API V.B1]

---

---
tags: FVM SDK + API V.B1
---

# SDK

### Initialize

```javascript=
const fvm = require('fvm-sdk');

const client = new fvm.Client('http://127.0.0.1:8545', 'main')
```

## Account

### Create/import account

```javascript=
// Generate raw private key
const account = fvm.create_account()

// Recovery accounts
const account = fvm.create_account_from_mnemonic(mnemonic, der_path = '...')
const account = fvm.create_account_from_privkey(privkey)
```

### Export key

```javascript=
const rawSecret = account.export_privkey() // As Buffer
const rawSecret = account.export_privkey_as_WIF() // As Buffer of utf-8 encoded of WIF

if (account.has_mnemonic()) {
    const rawMnemonic = account.export_mnemonic() // String
}
```

### Connect hardware wallet

```javascript=
const account = fvm.create_account_from_hardware()
const address = account.address()

// Hardware account is not exportable
assert.ok(!account.exportable())
```

## Other

### Get balance

```javascript=
const balance = client.getBalance(address | account);
const tokenBalance = client.tokenBalance(tokenAddress, address | account);
```

### Create transaction

```javascript=
const address = account.address()
const receiver = "...";
const token = "...";
const contract = "...";
const calldata = "...";

// Basic
const txId1 = client.sendTo(from=address, to=receiver, amount=1);
const txId2 = client.sendTokenTo(from=address, to=receiver, token=token, amount=1.1);

// Basic: interact with contract
const abi = <json>
const contract = client.contract(abi, contract);
const approveTx = await contract.methods.approve(receiver, 1.1).send(from=account);
const balance = await contract.methods.balanceOf(address).call()

// Adjust transaction
let txBuilder = new TransactionBuilder();
let utxos = new client.getUtxos(address)
let rawTransaction = txBuilder
        .withUtxos(utxos)
        .toAddress(receiver, 1.2)
        .withCalldata(contract, calldata);
        .changeTo(address)
        .sign(acount, ...);
const txId = client.sendRawTransaction(rawTransaction)
```

### Receive funds using QR code

```javascript=
const Utils = require("fvm-utils");

const nativePaymentData = Utils.generate_payment_message(to, amount);
const paymentData = Utils.generate_payment_message(to, amount, token);

// Use paymentData(Hex) to generate QR code

let transactionBuilder = Utils.decode_payment_message(paymentData);
rawTransaction = transactionBuilder
    .withUtxos(utxos)
    .changeTo(address)
    .sign(account, ...);
const txId = client.sendRawTransaction(rawTransaction);
```

### Buy token

```javascript=
// TODO
```

### Query asset

```javascript=
const balance = client.getBalance(address);
const tokenBalance = client.getTokenBalance(token, address);
```

# Backend

### Get transactions history by address to query activities

```javascript=
curl https://api.next.org/transactions/<address>
`
[
    {
        ...Transaction
    }
]
`
```




