near-api-ts
v0.5.1
Published
Typescript library for interactions with Near Protocol in the browser or Node.js
Readme
NEAR API TS
TypeScript library for interactions with Near Protocol in the browser or Node.js
Installation
pnpm add near-api-tsNode.js (esm, 22+) / Browser
import {
createTestnetClient,
createMemoryKeyService,
createMemorySigner,
transfer,
near,
isNatError,
} from 'near-api-ts';
// Read some data from the chain
const client = await createTestnetClient();
const { accountInfo } = await client.getAccountInfo({
accountId: 'testnet',
atMomentOf: 'LatestFinalBlock',
})
console.log(accountInfo.balance.total.near)
console.log(accountInfo.balance.total.yoctoNear)
// Send some transaction
const keyService = await createMemoryKeyService({
keySource: { privateKey: 'ed25519:your-private-key' } ,
});
const signer = await createMemorySigner({
signerAccountId: 'your-account.testnet',
client,
keyService,
});
await signer.executeTransaction({
intent: {
action: transfer({ amount: { yoctoNear: '1' } }),
receiverAccountId: 'some-receiver.testnet',
}
});
// Handle transaction errors
try {
const txResult = await signer.executeTransaction({
intent: {
action: transfer({ amount: near('10000000000') }),
receiverAccountId: 'some-receiver.testnet',
},
});
} catch (e) {
if (isNatError(e, 'MemorySigner.ExecuteTransaction.Rpc.Transaction.Signer.Balance.TooLow')) {
console.log(e.context.transactionCost)
}
}
// Use library functions in 'safe' mode
const maybeBlock = await client.safeGetBlock({
blockReference: { blockHeight: 10000000000000 },
});
console.log(maybeBlock); // { ok: false, error: NatError: <{ kind: 'Client.GetBlock.Rpc.Block.NotFound', context: null }> }You may found more examples in the examples/nodejs/ts-esm folder
