ANS
  • ANS Description
  • Roadmap
  • How it works
  • Tokenomics
  • Testnets
  • Integration
Powered by GitBook
On this page
  • ANS SDK
  • Install
  • Usage
  • React snippet example

Integration

Information for integrating with the Alephium Name Service protocol.

PreviousTestnets

Last updated 10 months ago

ANS SDK

Use the ANS SDK to integrate in your JS/TS projects.

Install

npm install @alph-name-service/ans-sdk

Usage

import { ANS } from "@alph-name-service/ans-sdk";

const ans = new ANS("mainnet");
const profile = await ans.getProfile("<address>");
// returns {name: "...", imgUri: "..."}
// if profile exists, undefined otherwise
const record = await ans.getRecord("<name>");
// returns {owner: ..., manager: ..., ttl: ...}
// if name exists, undefined otherwise
const resolvedAddress = await ans.resolveName("<name>");
// returns the resolved address string
// if name exists, undefined otherwise

If you want to integrate within a more involved use-case where you need to batch and get info for multiple addresses per request, feel free to reach out in Discord, Telegram or Twitter.

React snippet example

import { ANS } from "@alph-name-service/ans-sdk";

const [ansName, setAnsName] = useState<string>();
const [ansUri, setAnsUri] = useState<string>();

const fetchAnsProfile = async (address) => {
	try {
		const ans = new ANS('mainnet');
		const profile = await ans.getProfile(address);
		if (profile?.name) {
			setAnsName(profile.name);
		}
		if (profile?.imgUri) {
			setAnsUri(profile.imgUri);
		}
	} catch (error) {
		console.error('Error fetching ANS:', error);
	}
};

useEffect(() => {
	if (wallet?.account?.address) {
		fetchAnsProfile(wallet.account.address);
	}
}, [wallet?.account?.address]);
npm: @alph-name-service/ans-sdknpm
Logo