Requirements
- Node.js v16^. Download Node.js.
- Chrome/Firefox. NEAR-API-JS is designed to run in the browser, but it requires some Node.js polyfills. Website bundlers like Webpack5 and Vite have removed these polyfills to reduce default bundle size. Check 'Caveats' to see how to handle such cases.
Config
Caveats
If installing on a browser, make sure that the client environment has access to .global & Buffer. If not you will get errors in the browser console.
Set .global & Buffer in your index.js
import { Buffer } from 'buffer'
if (window) {
window.global = {}
window.process = { env: {}}
window.exports = {}
window.Buffer = Buffer
}
<script type="application/javascript" src="https://cdn.jsdelivr.net/gh/Danail-Irinkov/bufferUMD@master/dist/bundle.min.js"></script>
<script>
if (window) {
window.global = {}
window.process = { env: {} }
window.exports = {}
window.Buffer = window.BufferUMD.Buffer
}
</script>
Firebase Functions
Due to missing browser polyfills 'near-api-js' will not be able to compose and URL and redirect the User to the wallet out of the box
The methods that redirect to the NEAR Waller are meant for Browser Usage However, if you need to get the redirect URL as a string, please refer to our RPC Login Documentation
Configuring Endpoints
You should create a function like this to switch the configuration between development and production environment
function getConfig (env) {
switch (env) {
case 'production':
case 'mainnet':
return {
networkId: 'mainnet',
nodeUrl: 'https://rpc.mainnet.near.org',
walletUrl: 'https://wallet.near.org',
helperUrl: 'https://helper.mainnet.near.org'
}
case 'development':
case 'testnet':
return {
networkId: 'testnet',
nodeUrl: 'https://rpc.testnet.near.org',
walletUrl: 'https://wallet.testnet.near.org',
helperUrl: 'https://helper.testnet.near.org'
}
default:
throw Error(`Unconfigured environment '${env}'`)
}
}
function getConfig (env) {
switch (env) {
case 'production':
case 'mainnet':
return {
networkId: 'mainnet',
nodeUrl: 'https://rpc.mainnet.near.org',
walletUrl: 'https://wallet.near.org',
helperUrl: 'https://helper.mainnet.near.org'
}
case 'development':
case 'testnet':
return {
networkId: 'testnet',
nodeUrl: 'https://rpc.testnet.near.org',
walletUrl: 'https://wallet.testnet.near.org',
helperUrl: 'https://helper.testnet.near.org'
}
default:
throw Error(`Unconfigured environment '${env}'`)
}
}
Creating an API client
import * as nearAPI from 'near-api-js'
let config = getConfig('testnet')
let keyStore = new nearAPI.keyStores.BrowserLocalStorageKeyStore()
config.deps = { keyStore: keyStore }
let near = await nearAPI.connect(config);
let walletAccount = new nearAPI.WalletAccount(near);
let accountId = walletAccount.getAccountId();
import * as nearAPI from 'near-api-js'
let config = getConfig('testnet')
let access_key = 'ed25519:30J08h8380h38U3J038z3830ub3U03J3030389H783g3Gg38g7G3G33JIG3O3KLJONCDO3NP2M2P3Mldsjdkfjdk'
const keyStore = new nearAPI.keyStores.InMemoryKeyStore()
const keyPair = nearAPI.KeyPair.fromString(access_key)
await keyStore.setKey(network, account, keyPair)
config.deps = { keyStore: keyStore }
let near = await nearAPI.connect(config);
let walletAccount = new nearAPI.WalletAccount(near);
let accountId = walletAccount.getAccountId();
<script src="https://cdn.jsdelivr.net/npm/near-api-js@0.41.0/dist/near-api-js.min.js"></script>
<script>
const near = new nearApi.Near({
keyStore: new nearApi.keyStores.BrowserLocalStorageKeyStore(),
networkId: 'testnet',
nodeUrl: 'https://rpc.testnet.near.org',
walletUrl: 'https://wallet.testnet.near.org'
});
const wallet = new nearApi.WalletConnection(near, 'my-app');
const contract = new nearApi.Contract(wallet.account(), 'devtest.testnet', {
viewMethods: ['whoSaidHi'],
changeMethods: ['sayHi']
});
</script>
Learn more
NEAR Features
Official 'near-api-js' Documentation