This guide walks you through creating and using offchain ENS subnames with the @thenamespace/offchain-manager SDK. You’ll be able to create a subname, and then read records.
Install the Offchain SDK and dotenv to load your API key from an environment variable:
npm
yarn
Copy
npm install @thenamespace/offchain-manager dotenv
Initialise the client with the network you want to use and your API key. We recommend setting your API key via an environment variable and keeping it secret by using server-side code.
Copy
import 'dotenv/config';import { createOffchainClient } from '@thenamespace/offchain-manager';// Required: set NAMESPACE_API_KEY in your environmentconst API_KEY = process.env.NAMESPACE_API_KEY as string;if (!API_KEY) throw new Error('Missing NAMESPACE_API_KEY');// Use 'sepolia' for testing, 'mainnet' for productionexport const client = createOffchainClient({ mode: 'mainnet', timeout: 5000, defaultApiKey: API_KEY,});console.log('Offchain client initialized');
2
Check if a subname is available
Use isSubnameAvailable before creating a subname to avoid overwriting an existing one.
Retrieve a single text record or all text records on a subname.
Copy
import { client } from './index';async function main() { const subname = 'alice.myensname.eth'; // You can retrieve all text records on a subname const all = await client.getTextRecords(subname); // You can also retrieve a single text record based on a key on a subname const { record: name } = await client.getTextRecord(subname, 'name'); console.log('all text records:', all); console.log('name:', name);}main();
Example output:
Copy
all text records: { name: 'Alice', url: 'https://example.com' }name: Alice
6
Look up address records
Address records are included in the subname response. A simple way to read them is via getFilteredSubnames and then inspecting addresses.
Subnames created with the Offchain SDK can be accessed and resolved using any of the eligible ENS client libraries like wagmi, viem, ethers, and others listed in the ENS Tools & Libraries documentation.
Reverse Resolution Limitation: Offchain subnames do not support reverse resolution, so you cannot fetch a profile from an address. However, you can still set address records on offchain subnames for forward resolution.