Documentation for agents: an index at /llms.txt, every docs page in one file at /llms-full.txt, and Markdown for any docs page by adding .md to its URL.

Skip to Content
ensforgeSDK

Get a website

Read a name's content hash and turn it into a gateway link to the IPFS, Arweave or Swarm site or file that the name points to.

By the end of this guide you can read the content hash of a name (a pointer to decentralized content, such as a website on IPFS) and turn it into a link a browser can open.

Prerequisites

Read the content hash

record.getContentHash reads the content hash record (ENSIP-7) and decodes it.

Example (Reading a Website)

get-website.ts
import { ensActions, Ensforge } from "@ensforge/sdk";
import { createPublicClient, http } from "viem";
import { mainnet } from "viem/chains";

const ens = Ensforge.create({
  publicClient: createPublicClient({ chain: mainnet, transport: http() }),
}).extend(ensActions());

const site = await ens.record.getContentHash({ name: "vitalik.eth" });
console.log(site?.uri);

The result is null when the record is not set or cannot be decoded. Otherwise it has three fields:

FieldDescription
protocolThe scheme: ipfs, ipns, bzz (Swarm), onion, onion3, sia (Skynet) or ar (Arweave).
valueThe identifier without the scheme, such as an IPFS CID.
uriprotocol://value.

See record.getContentHash.

A uri such as ipfs://… is not a link a browser opens. ContentHash.toGatewayUrl turns it into an HTTP URL through a storage gateway.

Example (Building a Gateway Link)

website-link.ts
import { ContentHash, ensActions, Ensforge } from "@ensforge/sdk";
import { createPublicClient, http } from "viem";
import { mainnet } from "viem/chains";

const ens = Ensforge.create({
  publicClient: createPublicClient({ chain: mainnet, transport: http() }),
  gateways: { ipfs: "https://gateway.pinata.cloud" },
}).extend(ensActions());

const site = await ens.record.getContentHash({ name: "vitalik.eth" });
const url = site && ContentHash.toGatewayUrl(site.uri, ens.config.gateways);
  • ens.config.gateways holds the client's gateways, so the link uses the gateway you configured.
  • ipfs:// and ipns:// go through the IPFS gateway, ar:// through the Arweave gateway and bzz:// through the Swarm gateway.
  • Unset gateways are the public defaults: https://ipfs.io, https://arweave.net and https://api.gateway.ethswarm.org.
  • A URI with no gateway form (onion://, sia://) comes back unchanged.

The public ipfs.io gateway is rate limited, so set your own in production. See Configure RPC and gateways.

Options

OptionTypeDefaultDescription
namestringrequiredThe name to read. Normalised for you.

The action also takes the options every read takes: signal, strict, gatewayUrls and blockNumber or blockTag.

Troubleshooting

The result is null

The name has no content hash, no resolver, or the bytes use a codec ensforge does not decode. With strict: true, a missing record fails with RECORD_NOT_FOUND or RESOLVER_NOT_FOUND.

INVALID_CONTENT_HASH

ContentHash.encode and ContentHash.decode throw INVALID_CONTENT_HASH when a value is malformed or uses an unsupported scheme. The read itself returns null instead.

The gateway is rate limiting or down. Pass a gateway you control in gateways when you create the client.

Was this page helpful?