How to Resolve ENS Domains Given a Wallet Address

In this tutorial, we’ll be using Alchemy’s NFT API to fetch all ENS Domain Names owned by a user.

📘

API Endpoint

This tutorial uses the getNFTs endpoint.

If you just need the script for this tutorial refer to the below Recipe or continue reading for more

The Alchemy SDK makes it possible to parse ENS names from an address and perform the reverse to get the address of an ENS name. This latter case is useful when there's a need to get more data from an (Address), such as contracts which have been deployed by the Wallet Address.

The (ENS)’s job is to map human-readable names like ‘alice.eth’ to machine-readable identifiers such as Ethereum addresses, other cryptocurrency addresses, content hashes, and metadata.

In this guide, we walk you through How to get an ENS name from an EOA.

📘

Looking to get an address from an ENS domain?

Read the guide on How to Resolve a Wallet Address Domains Given an ENS instead.

What tools do you need to find an ENS name?

  1. Create a free Alchemy account to access the NFT API Endpoints
  2. Select a wallet address to request ENS domains from (i.e. “0x…XXX”)
  3. Alchemy SDK (recommended)

How do you find a wallet’s owned ENS names?

Because all ENS domains are ERC721 tokens (NFTs), NFT API is a powerful tool for querying blockchains for data about wallets that own ENS domains. Let’s set up our basic environment in your code editor and terminal by following instructions as mentioned in the NFT API Quickstart Guide. We’ll be using npm in this example.

1. Create a new repository with Alchemy's NFT API

Open up a terminal, and from the command line, create a new repository to hold your quickstart scripts. We'll also initialize the repo as an npm project.

mkdir alchemy-nft-api
cd alchemy-nft-api
npm init --yes

2. Install the Alchemy SDK

Navigate into your project directory and run:

npm install alchemy-sdk

3. [optional] Load ES6 modules

If you run into module errors, you may need to add 'type':'module' to your package.json because we'll be utilizing import syntax to load ES6 modules in our script.

{
...
"type": "module"
}

4. Write script using getNFTs to get ENS domains

To get the ENS domains for a given address, we'll make a request to getNFTs with contract filtering The contract that we'll specify in the request is the address for all ENS contracts: 0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85

In this example we'll get the owned ENS domains for address 0x458d1E307CcA61C0Bea82f7663F66831175EcDe8

Create a file in your repository called resolve-ens.js and paste in the following

// Setup: npm install alchemy-sdk
import { Alchemy, Network } from "alchemy-sdk";

const config = {
  apiKey: "<-- ALCHEMY APP API KEY -->",
  network: Network.ETH_MAINNET,
};
const alchemy = new Alchemy(config);

const walletAddress = "0x458d1E307CcA61C0Bea82f7663F66831175EcDe8"; // replace with wallet address
const ensContractAddress = "0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85";
const nfts = await alchemy.nft.getNftsForOwner(walletAddress, {
  contractAddresses: [ensContractAddress],
});

console.log(nfts);

5. Print ENS objects

From the command line, run node resolve-ens.js :

{
  ownedNfts: [
    {
      contract: [Object],
      id: [Object],
      balance: '1',
      title: 'elanhalpern.eth',
      description: 'elanhalpern.eth, an ENS name.',
      tokenUri: [Object],
      media: [Array],
      metadata: [Object],
      timeLastUpdated: '2022-03-07T07:50:03.443Z'
    },
    {
      contract: [Object],
      id: [Object],
      balance: '1',
      title: 'cryptocreamery.eth',
      description: 'cryptocreamery.eth, an ENS name.',
      tokenUri: [Object],
      media: [Array],
      metadata: [Object],
      timeLastUpdated: '2022-03-05T06:21:12.583Z'
    }
  ],
  totalCount: 2,
  blockHash: '0xc983065bae1b5a52a87b3bdce7f3669cb56f9f843c348ffe44d23742bf5fff76'
}

The title field in our ownedNFT objects is the name of the ENS domain. In this case, the address owns two ENS domains:

  • elanhalpern.eth
  • cryptocreamery.eth

And that's it! You can now easily get any ENS domains from a given wallet address :tada:

Sometimes, after you have successfully resolved the ENS domain name, you may want to parse the Wallet address of the ENS. This can be achieved using the Alchemy core SDK resolveName method.

📘

How to Resolve a Wallet Address Domains Given an ENS

Read the guide on How to Resolve a Wallet Address Domains Given an ENS.


ReadMe