Skip to main content

JavaScript SDK quickstart

Use JavaScript SDK v4 to resolve a .sns domain and query domain data for a wallet.

Setup

Complete the JavaScript SDK installation, then create the shared RPC connection:

import { Connection } from "@solana/web3.js";

const connection = new Connection("https://your-rpc-endpoint.example");

Common usage

Resolve a domain

Pass a full .sns name to resolve:

import { resolve } from "@bonfida/spl-name-service";

const owner = await resolve(connection, "name.sns");
console.log(owner.toBase58());
// => "<BASE58_PUBLIC_KEY>"

resolve returns the effective owner as a web3.js PublicKey.

For .sol resolution that must verify the SRS and corresponding SNS targets match when SRS-backed resolution is enabled in a future update, use safeResolve(connection, "name.sol") instead.

Get a primary domain

Use getPrimaryDomain with the wallet public key:

import { getPrimaryDomain } from "@bonfida/spl-name-service";
import { PublicKey } from "@solana/web3.js";

const wallet = new PublicKey("<WALLET_ADDRESS>");
const primary = await getPrimaryDomain(connection, wallet);

if (!primary.stale) {
console.log(`${primary.reverse}.sns`);
// => "name.sns"
}

reverse is TLD-less. Append .sns for display. Do not present a stale result as the wallet's current primary domain.

Get domains for an owner

Use getSnsDomainsForOwner to retrieve top-level domains owned by the wallet:

import { getSnsDomainsForOwner } from "@bonfida/spl-name-service";
import { PublicKey } from "@solana/web3.js";

const wallet = new PublicKey("<WALLET_ADDRESS>");
const domains = await getSnsDomainsForOwner(connection, wallet);
const suffixedDomains = domains.map(({ domain }) => `${domain}.sns`);

console.log(suffixedDomains);
// => ["name.sns", "another-name.sns"]

Each result contains a TLD-less domain and its name-account key. Results omit domains without reverse data and do not include tokenized domains or subdomains.

Next steps