Steps to manage dns records in DNSMadeEasy with REST API
Authentication Header
The following Request Headers are required by refering to the API Document
- x-dnsme-apiKey – The API Key for your account
- x-dnsme-requestDate – Standard HTTP-formatted date. This date is used to protect against falsified requests played back at a future time.
- x-dnsme-hmac – HMAC hash of value of the x-dnsme-requestDate field
Login to https://cp.dnsmadeeasy.com to get the API Key
Prepare a PowerShell Function to generate the Authentication Header
function Get-DnsMadeEasyHeader {
#Replace the APIKey & SecretKey with the Value from your account
$APIKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
$secretKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
$Date = ([DateTime](Get-Date).ToUniversalTime()).ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'")
$HMACSHA1 = New-Object System.Security.Cryptography.HMACSHA1
$HMACSHA1.Key = [System.Text.Encoding]::ASCII.GetBytes($secretKey)
$HMACHash = ([System.BitConverter]::ToString($HMACSHA1.ComputeHash([Text.Encoding]::ASCII.GetBytes($Date))) -Replace "-", "").ToLower()
$Headers = @{ 'x-dnsme-apiKey' = $APIKey
'x-dnsme-requestDate' = $Date
'x-dnsme-hmac' = $HMACHash
'ContentType' = "application/json"}
Return $Headers
}
List All Domains Registered
List all domains registered in our DnsMadeEasy Account
#Get All Domain Registered
$URI = "https://api.dnsmadeeasy.com/V2.0/dns/managed"
$Response = Invoke-RestMethod -Uri $URI -Headers (Get-DnsMadeEasyHeader)-Method Get
$Response.data | Select Name, ID
Output
name id
---- --
aventislab.com xxxxxxx
aventislab.info xxxxxxx
aventistech.com xxxxxxx
powershell365.com xxxxxxx
yongkw.com xxxxxxx
List All Records for Particular Domain
List all records, like A, MX, TXT and others for a particular domain, like yongkw.com
#List Records for 1 Domain
$DomainName = "YongKW.com"
$DomainID = ($Response.data | ? Name -like $DomainNAme).id
$DNS_Records = Invoke-RestMethod -Uri https://api.dnsmadeeasy.com/V2.0/dns/managed/$DomainID/records -Headers (Get-DnsMadeEasyHeader) -Method Get
#Output of All Records
$DNS_Records.data | Select ID,Name, Type, Value
Output
id name type value
-- ---- ---- -----
111555619 MX mail
107390283 A 146.66.105.253
111643122 TXT "1234"
111555587 mail A 121.121.43.51
107870636 my A 121.121.43.52
108219864 uat A 47.254.232.92
108854884 unifi A 121.121.43.52
Create a New Record
Create a new TXT record with value = "1234567890" in yongkw.com
#Create a New Record
$DomainName = "YongKW.com"
$DomainID = ($Response.data | ? Name -like $DomainNAme).id
#Data Declare here need to be converted to json format later
$Body = @{
name = ""
type = "TXT"
value = "1234567890"
id = $ID
gtdLocation = 'DEFAULT'
ttl = 86400
}
Invoke-RestMethod -Uri https://api.dnsmadeeasy.com/V2.0/dns/managed/$DomainID/records -Headers (Get-DnsMadeEasyHeader) -Body ($Body | ConvertTo-Json) -Method Post
#Verify the DNS record is created successfully
$DNS_Records = (Invoke-RestMethod -Uri https://api.dnsmadeeasy.com/V2.0/dns/managed/$DomainID/records -Headers (Get-DnsMadeEasyHeader) -Method Get).Data
$DNS_Records | ? Type -Like "TXT" | Select Name, Type, Value
Output
name type value
---- ---- -----
TXT "1234"
Update DNS Record
Update TXT record with new value
$DNS_Records = (Invoke-RestMethod -Uri https://api.dnsmadeeasy.com/V2.0/dns/managed/$DomainID/records -Headers (Get-DnsMadeEasyHeader) -Method Get).Data
#Search for the DNS record need to be updated
$ID = ($DNS_Records | Where-Object {($_.type -eq "TXT")}).ID
$New_Value = "12345"
$Body = @{
name = ""
type = "TXT"
value = $New_Value
id = $ID
gtdLocation = 'DEFAULT'
ttl = 86400
}
#Update DNS Record
Invoke-RestMethod -Uri https://api.dnsmadeeasy.com/V2.0/dns/managed/$DomainID/records/$ID -Headers (Get-DnsMadeEasyHeader) -Body ($Body | ConvertTo-Json) -Method Put
#Verify TXT record is updated
$DNS_Records = (Invoke-RestMethod -Uri https://api.dnsmadeeasy.com/V2.0/dns/managed/$DomainID/records -Headers (Get-DnsMadeEasyHeader) -Method Get).Data
$DNS_Records | Where-Object {($_.type -eq "TXT")}
Output
name type value
---- ---- -----
TXT "12345"
Delete DNS Record
Delete the TXT record
$DNS_Records = (Invoke-RestMethod -Uri https://api.dnsmadeeasy.com/V2.0/dns/managed/$DomainID/records -Headers (Get-DnsMadeEasyHeader) -Method Get).Data
#Search for the DNS record need to be deleted
$ID = ($DNS_Records | Where-Object {($_.type -eq "TXT") -and ($_.value -cmatch "1234")}).ID
#Delete the DNS record
Invoke-RestMethod -Uri https://api.dnsmadeeasy.com/V2.0/dns/managed/$DomainID/records/$ID -Headers (Get-DnsMadeEasyHeader) -Method Delete