|
| 1 | +# Set Names (Batch) |
| 2 | + |
| 3 | +This POST route creates multiple names (subdomains) for a given domain in a single transaction. This is a batch version of the `set-name` endpoint that provides significant performance improvements when setting multiple names at once. |
| 4 | + |
| 5 | +## Performance Benefits |
| 6 | + |
| 7 | +Using `set-names` instead of multiple `set-name` calls provides: |
| 8 | +- **60-80% faster execution** for multiple names |
| 9 | +- **Single API key validation** instead of N validations |
| 10 | +- **Single domain lookup** instead of N lookups |
| 11 | +- **Transactional safety** - all names succeed or all fail |
| 12 | +- **Reduced network overhead** - one HTTP request instead of N requests |
| 13 | + |
| 14 | +## Parameters |
| 15 | + |
| 16 | +| Parameter | Type | Required | Description | |
| 17 | +| --------- | ------ | -------- | ---------------------------------------------------------------------------------------------------------------------------- | |
| 18 | +| `domain` | string | Yes | The domain (e.g. "testbrand.eth"). | |
| 19 | +| `names` | array | Yes | Array of name objects to create/update. Each object has the same structure as the `set-name` endpoint. | |
| 20 | + |
| 21 | +## Name Object Structure |
| 22 | + |
| 23 | +Each object in the `names` array supports the same parameters as the `set-name` endpoint: |
| 24 | + |
| 25 | +| Parameter | Type | Required | Description | |
| 26 | +| -------------- | ------ | -------- | ---------------------------------------------------------------------------------------------------------------------------- | |
| 27 | +| `name` | string | Yes | The name being set, i.e., the "example" in example.testbrand.eth. | |
| 28 | +| `address` | string | No | The Ethereum address the name points to. | |
| 29 | +| `contenthash` | string | No | The link for an [IPFS](https://docs.ipfs.tech/) or [IPNS](https://docs.ipfs.tech/concepts/ipns/#mutability-in-ipfs) website. | |
| 30 | +| `text_records` | object | No | An object containing key-value pairs of the text records to be set. | |
| 31 | +| `coin_types` | object | No | An object containing key-value pairs of L2 chains and their resolved address. | |
| 32 | + |
| 33 | +## Batch Processing |
| 34 | + |
| 35 | +- **Transaction Safety**: All names are processed in a single database transaction |
| 36 | +- **All-or-Nothing**: If any name fails, the entire batch is rolled back |
| 37 | +- **Name Limits**: The batch size is validated against your domain's name limit |
| 38 | +- **Mixed Operations**: Can create new names and update existing names in the same batch |
| 39 | + |
| 40 | +## Response Format |
| 41 | + |
| 42 | +### Success Response |
| 43 | + |
| 44 | +```json |
| 45 | +{ |
| 46 | + "success": true, |
| 47 | + "processed": 3, |
| 48 | + "results": [ |
| 49 | + { |
| 50 | + "index": 0, |
| 51 | + "name": "alice", |
| 52 | + "success": true, |
| 53 | + "subdomainId": 123 |
| 54 | + }, |
| 55 | + { |
| 56 | + "index": 1, |
| 57 | + "name": "bob", |
| 58 | + "success": true, |
| 59 | + "subdomainId": 124 |
| 60 | + }, |
| 61 | + { |
| 62 | + "index": 2, |
| 63 | + "name": "charlie", |
| 64 | + "success": true, |
| 65 | + "subdomainId": 125 |
| 66 | + } |
| 67 | + ] |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### Error Response |
| 72 | + |
| 73 | +```json |
| 74 | +{ |
| 75 | + "error": "Batch operation failed", |
| 76 | + "processed": 0, |
| 77 | + "errors": [ |
| 78 | + { |
| 79 | + "index": 1, |
| 80 | + "name": "invalid..name", |
| 81 | + "error": "Invalid ens name" |
| 82 | + } |
| 83 | + ], |
| 84 | + "total": 3 |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +## Multichain Address Resolution |
| 89 | + |
| 90 | +Namestone supports address resolution on any [L2 Chains Supported by ENS](https://github.com/ensdomains/address-encoder/blob/master/docs/supported-cryptocurrencies.md). |
| 91 | +To add an address to an L2 chain use its coin_type. (See coin_type column in the above link) . |
| 92 | +Or convert chain_id to coin_type using the following [typescript template](https://stackblitz.com/edit/stackblitz-starters-mfk6i5?file=src%2Findex.ts). |
| 93 | + |
| 94 | +## Curl Example |
| 95 | + |
| 96 | +```bash |
| 97 | +curl -X POST \ |
| 98 | + -H 'Content-Type: application/json' \ |
| 99 | + -H 'Authorization: YOUR_API_KEY' \ |
| 100 | + -d '{ |
| 101 | + "domain": "namestone.xyz", |
| 102 | + "names": [ |
| 103 | + { |
| 104 | + "name": "alice", |
| 105 | + "address": "0x534631Bcf33BDb069fB20A93d2fdb9e4D4dD42CF", |
| 106 | + "text_records": { |
| 107 | + "com.twitter": "alice_crypto", |
| 108 | + "description": "Alice profile", |
| 109 | + "avatar": "https://imagedelivery.net/UJ5oN2ajUBrk2SVxlns2Aw/alice-avatar.png" |
| 110 | + } |
| 111 | + }, |
| 112 | + { |
| 113 | + "name": "bob", |
| 114 | + "address": "0x1234567890123456789012345678901234567890", |
| 115 | + "coin_types": { |
| 116 | + "2147483785": "0x1234567890123456789012345678901234567890", |
| 117 | + "2147492101": "0x1234567890123456789012345678901234567890" |
| 118 | + }, |
| 119 | + "text_records": { |
| 120 | + "com.github": "bob-dev", |
| 121 | + "url": "https://bob.dev" |
| 122 | + } |
| 123 | + }, |
| 124 | + { |
| 125 | + "name": "charlie", |
| 126 | + "address": "0x9876543210987654321098765432109876543210", |
| 127 | + "contenthash": "ipfs://QmYourContentHash" |
| 128 | + } |
| 129 | + ] |
| 130 | + }' \ |
| 131 | + https://namestone.com/api/public_v1/set-names |
| 132 | +``` |
| 133 | + |
| 134 | +## SDK Example |
| 135 | + |
| 136 | +```typescript |
| 137 | +import NameStone, { AuthenticationError, NetworkError, TextRecords, CoinTypes } from '@namestone/namestone-sdk'; |
| 138 | + |
| 139 | +// Initialize the NameStone instance |
| 140 | +const ns = new NameStone('<YOUR_API_KEY_HERE>'); |
| 141 | + |
| 142 | +// Define the domain |
| 143 | +const domain = "namestone.xyz"; |
| 144 | + |
| 145 | +// Define the batch of names to set |
| 146 | +const names = [ |
| 147 | + { |
| 148 | + name: "alice", |
| 149 | + address: "0x534631Bcf33BDb069fB20A93d2fdb9e4D4dD42CF", |
| 150 | + text_records: { |
| 151 | + "com.twitter": "alice_crypto", |
| 152 | + "description": "Alice profile", |
| 153 | + "avatar": "https://imagedelivery.net/UJ5oN2ajUBrk2SVxlns2Aw/alice-avatar.png" |
| 154 | + } |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "bob", |
| 158 | + address: "0x1234567890123456789012345678901234567890", |
| 159 | + coin_types: { |
| 160 | + "2147483785": "0x1234567890123456789012345678901234567890", // Arbitrum |
| 161 | + "2147492101": "0x1234567890123456789012345678901234567890" // Polygon |
| 162 | + }, |
| 163 | + text_records: { |
| 164 | + "com.github": "bob-dev", |
| 165 | + "url": "https://bob.dev" |
| 166 | + } |
| 167 | + }, |
| 168 | + { |
| 169 | + name: "charlie", |
| 170 | + address: "0x9876543210987654321098765432109876543210", |
| 171 | + contenthash: "ipfs://QmYourContentHash" |
| 172 | + } |
| 173 | +]; |
| 174 | + |
| 175 | +// Use an immediately invoked async function to allow top-level await |
| 176 | +(async () => { |
| 177 | + try { |
| 178 | + const response = await ns.setNames({ |
| 179 | + domain: domain, |
| 180 | + names: names |
| 181 | + }); |
| 182 | + |
| 183 | + console.log("Names set successfully:", response); |
| 184 | + console.log(`Processed ${response.processed} names`); |
| 185 | + |
| 186 | + // Process results |
| 187 | + response.results.forEach((result, index) => { |
| 188 | + console.log(`Name ${result.name} (index ${result.index}): ${result.success ? 'Success' : 'Failed'}`); |
| 189 | + }); |
| 190 | + |
| 191 | + } catch (error) { |
| 192 | + if (error instanceof AuthenticationError) { |
| 193 | + console.error("Authentication failed:", error.message); |
| 194 | + } else if (error instanceof NetworkError) { |
| 195 | + console.error("Network error:", error.message); |
| 196 | + } else { |
| 197 | + console.error("An unexpected error occurred:", error); |
| 198 | + |
| 199 | + // Handle batch-specific errors |
| 200 | + if (error.response?.data?.errors) { |
| 201 | + console.error("Individual name errors:"); |
| 202 | + error.response.data.errors.forEach((err) => { |
| 203 | + console.error(` - Index ${err.index} (${err.name}): ${err.error}`); |
| 204 | + }); |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | +})(); |
| 209 | +``` |
| 210 | + |
| 211 | +## Error Handling |
| 212 | + |
| 213 | +The `set-names` endpoint provides detailed error information: |
| 214 | + |
| 215 | +### Common Error Scenarios |
| 216 | + |
| 217 | +1. **Invalid Names Array** |
| 218 | + ```json |
| 219 | + { "error": "Missing names array or empty array" } |
| 220 | + ``` |
| 221 | + |
| 222 | +2. **Individual Name Errors** |
| 223 | + ```json |
| 224 | + { "error": "Invalid ens name at index 2: invalid..name" } |
| 225 | + ``` |
| 226 | + |
| 227 | +3. **Name Limit Exceeded** |
| 228 | + ```json |
| 229 | + { "error": "Api name limit would be exceeded. Current: 5, Adding: 10, Limit: 12" } |
| 230 | + ``` |
| 231 | + |
| 232 | +4. **Transaction Failure** |
| 233 | + ```json |
| 234 | + { |
| 235 | + "error": "Batch operation failed", |
| 236 | + "processed": 0, |
| 237 | + "errors": [ |
| 238 | + { "index": 1, "name": "problematic-name", "error": "Specific error message" } |
| 239 | + ], |
| 240 | + "total": 5 |
| 241 | + } |
| 242 | + ``` |
| 243 | + |
| 244 | +## Best Practices |
| 245 | + |
| 246 | +1. **Batch Size**: While there's no hard limit, batches of 10-50 names provide optimal performance |
| 247 | +2. **Error Handling**: Always check the response for individual name errors |
| 248 | +3. **Validation**: Validate ENS names client-side before sending to reduce API calls |
| 249 | +4. **Rate Limiting**: Respect your API rate limits when sending large batches |
| 250 | +5. **Retry Logic**: Implement retry logic for network failures, but not for validation errors |
| 251 | + |
| 252 | +## Comparison with Single set-name |
| 253 | + |
| 254 | +| Feature | set-name | set-names | |
| 255 | +|---------|----------|-----------| |
| 256 | +| Names per request | 1 | Multiple | |
| 257 | +| API calls needed | N | 1 | |
| 258 | +| Transaction safety | Per name | All names | |
| 259 | +| Performance | Baseline | 60-80% faster | |
| 260 | +| Error handling | Individual | Batch + individual | |
| 261 | +| Name limit validation | Per request | Per batch | |
| 262 | + |
| 263 | +## Migration from set-name |
| 264 | + |
| 265 | +To migrate from multiple `set-name` calls to `set-names`: |
| 266 | + |
| 267 | +1. **Collect all name objects** into a single array |
| 268 | +2. **Replace multiple API calls** with one `set-names` call |
| 269 | +3. **Update error handling** to process batch responses |
| 270 | +4. **Test thoroughly** with your specific use case |
| 271 | + |
| 272 | +Example migration: |
| 273 | + |
| 274 | +```javascript |
| 275 | +// Before: Multiple set-name calls |
| 276 | +for (const nameData of names) { |
| 277 | + await ns.setName({ domain, ...nameData }); |
| 278 | +} |
| 279 | + |
| 280 | +// After: Single set-names call |
| 281 | +await ns.setNames({ domain, names }); |
| 282 | +``` |
0 commit comments