Skip to content

Instantly share code, notes, and snippets.

@Delivator
Forked from johnnywu-namebase/"Records" array
Last active August 19, 2020 02:18
Show Gist options
  • Save Delivator/8f5f2c3c49c5561818f59d293a666efe to your computer and use it in GitHub Desktop.
Save Delivator/8f5f2c3c49c5561818f59d293a666efe to your computer and use it in GitHub Desktop.
Accompanying code for "Setting DNS Records" Namebase blog post
{ ttl: 10800, type: ‘NS’, host: ‘ns1’, value: ‘44.231.6.183’ }
const credentials = Buffer.from(`${ACCESS_KEY}:${SECRET_KEY}`);
const encodedCredentials = credentials.toString('base64');
const authorization = `Basic ${encodedCredentials}`;
node namebaseApi.js put nameserver example ‘{ “records”: [], “deleteRecords”: [{ “type”: “TXT”, “host”: “foo” }] }’
const fetch = require('node-fetch');
const ACCESS_KEY = '';
const SECRET_KEY = '';
const credentials = Buffer.from(`${ACCESS_KEY}:${SECRET_KEY}`);
const encodedCredentials = credentials.toString('base64');
const authorization = `Basic ${encodedCredentials}`;
async function get(endpoint, body = null) {
const options = {
method: 'GET',
headers: {
Authorization: authorization,
Accept: 'application/json',
'Content-Type': 'application/json',
},
};
const url = `https://namebase.io${endpoint}`;
return fetch(url, options)
.then(res => res.json())
.catch(err => err);
}
async function put(endpoint, body) {
const options = {
method: 'PUT',
body: body,
headers: {
Authorization: authorization,
Accept: 'application/json',
'Content-Type': 'application/json',
},
};
const url = `https://namebase.io${endpoint}`;
return fetch(url, options)
.then(res => res.json())
.catch(err => err);
}
async function main() {
const args = process.argv.slice(2);
let func, path, data;
const domain = args[2];
if (args[0] === 'get') {
func = get;
} else if (args[0] === 'put') {
func = put;
if (args.length < 4) throw new Error('Put requires 4 arguments');
data = args[3];
} else {
throw new Error("Method should be either 'get' or 'put'");
}
if (args[1] === 'blockchain') {
path = `/api/v0/dns/domains/${domain}`;
} else if (args[1] === 'blockchain-advanced') {
path = `/api/v0/dns/domains/${domain}/advanced`;
} else if (args[1] === 'nameserver') {
path = `/api/v0/dns/domains/${domain}/nameserver`;
} else {
throw new Error("Service should be 'blockchain', 'blockchain-advanced' or 'nameserver'");
}
return func(path, data);
}
main().then(res => {
console.log(res);
});
node ./namebaseApi.js get blockchain YOUR_DOMAIN
node ./namebaseApi.js put blockchain YOUR_DOMAIN ‘{“records”: [{ “ttl”: 10800, “type”: “NS”, “host”: “ns1”, “value”: “44.231.6.183” }] }’
node namebaseApi.js put blockchain YOUR_DOMAIN ‘{ “records”: [{ “type”: “TXT”, “host”: “”, “value”: “[YOUR_SKYLINK]”, “ttl”: 0 }] }’
node ./namebaseApi.js METHOD SERVICE DOMAIN RECORD_DATA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment