0

EDIT: I am looking for assistance on the GoDaddy API and how to form the request so I can delete a record. I can replace records and add new records and, of course, read records. I do not need assistance on building the module. Just having a problem figuring out the REST API piece for doing a delete on GoDaddy with the API.

I found this: Error 422 When Using Powershell to Update DNS Records on GoDaddy API

This gave me a good start. I have a number of functions I have already written to create or modify various different types of record. I have learned a lot along the way. Something that I have been unable to figure out so far is how to use the API to delete an existing record that is no longer needed.

I discovered that if I had an existing record of the same name and type and I tried to create a new, it would replace what was there with the new value. If it is just an A record and that is your desire, that's great. But if it is an MX or NS record, that is probably not the desired result. I am working on the helper functions to make sure that I don't blow away existing records before I publish to my module.

I am relatively new to reading API documentation and working with REST API's so I am probably just missing something basic, but if anyone could provide me with some guidance on how to configure my call so that I can clean up records that are no longer needed.

I am also having some issues formatting the SRV record properly so that it doesn't error out when I make the call. I am not sure where my problem is and I am not seeing anything in the documentation of allowed/expected values for Protocol, Service, etc.

Here is an excerpt of my code where I am calling:

try{
    $ret = Invoke-WebRequest https://api.godaddy.com/v1/domains/domain/records/$type/$alias -method put -headers $headers -Body $json -ContentType "application/json"
    if ($ret.StatusCode -eq 200) { Write-Verbose -Message "Success!" }
    else { Write-Warning -Message "ERROR" }
} catch {
    $result = $_.Exception.Response.GetResponseStream()
    $reader = New-Object System.IO.StreamReader($result)
    $reader.BaseStream.Position = 0
    $reader.DiscardBufferedData()
    $responseBody = $reader.ReadToEnd();
    $responseBody = $responseBody | ConvertFrom-Json | Format-Custom | Out-String
    Write-Warning -Message "ERROR: $responseBody"
}

You will recognize this code block as it is largely taken intact from the aforementioned post. I am passing in $type as a valid record type (A, CNAME, etc.). $alias is the name of an existing record. $headers is a properly formatted header with a valid key/secret pair (I am able to Get and Put other records). I have tried omitting the body altogether to delete a value and that fails. I have tried doing the body with a JSON empty set ([], [{}]) as well as passing in data and TTL values with no value in the JSON. I can't get a successful query to work. I am testing with the following:

Invoke-WebRequest "https://api.godaddy.com/v1/domains/mydomain/records/A/test2" -method put -headers $header -ContentType "application/json" -Body '[{}]'

Any guidance or pointers to documentation that I haven't found would be greatly appreciated.

  • I'm not sure if you need help with the GoDaddy API or how to create PowerShell module, could you clarify which you need help with? – Persistent13 Oct 04 '17 at 21:43
  • I have added a note at the top that what I need help with is the formation of the API call, not how to create a PowerShell module. There are numerous guides on creating a module that I could follow, but this isn't my first module (at least internally). – PowerShell Submariner Oct 05 '17 at 12:22
  • It's not documented but have you tried the delete method against a DNS record? `Invoke-WebRequest "https://api.godaddy.com/v1/domains/mydomain/records/A/test2" -method delete -headers $header` – Persistent13 Oct 05 '17 at 15:09
  • I have not. I will try that and let you know. I did not realize the DELETE might be an option for the method. – PowerShell Submariner Oct 05 '17 at 19:54
  • I just tried it and it did not work but I don't know why. I did verify that the record I was trying to remove was an A record and had the valid name. The error message I get back is "Invoke-WebRequest : {"code":"NOT_FOUND","message":"Not Found : The requested resource was not found"}". – PowerShell Submariner Oct 05 '17 at 20:02

2 Answers2

0

I had the same problem, it's about JSON encoding. I will show you a PHP example you can adapt to anything. The format that GoDaddy will provide is like this:

[{"":"","":"","":"",.....}]

I don't know from where comes the [] but I remove them, the parse json with regular PHP functions:

$json = str_replace("]","",$json);

//print_r($json);

$json = json_decode($json,true);

Now you will get the usable object to work with.

When send data to GoDaddy, do the same (I use curl)

$data = '[{"type":"'.$type.'","name":"'.$name.'","data":"'.$data.'","ttl":'.$ttl.'}]';

A full example that I build for updating my home server dynamic IP :) Check the functions Update() and get()

https://github.com/yo3hcv/Dynamic-DNS-hosting-home-or-mobile-with-GoDaddy/blob/master/pip.php

yo3hcv
  • 1,531
  • 2
  • 17
  • 27
0

This maybe useful for others as I ran into the same unclarity about deleting a DNS record from a Godaddy domain using their API.

Godaddy doesn't support deleting an individual record.

From their support on the question whether it was possible to delete a record:

Dear Sir/Madam,

This API call does not exist at this time and it is something on our backlog to build out but there is no ETA to have it created. Current you can use the Put endpoint on the record type to replace all but the one you do not want to keep or you may go into our UI to remove it that way.

See their api documentation for what is available (at this date July 2020).

However, what you can do is replace all the existing records with a new set. So suppose you have three A records with subdomain SD1, SD2 and SD3 and you want to remove SD2. You could sent an update command using:

curl -X PUT "https://api.godaddy.com/v1/domains/<yourdomain>/records/A" -H "accept: application/json" -H "Content-Type: application/json" -H "Authorization: <yourAPIKEY>" -d "[ { \"data\": \"<IP for SD1>\", \"name\": \"SD1\", \"ttl\": 3600 }, { \"data\": \"<IP for SD3>\", \"name\": \"SD3\", \"ttl\": 3600 }]"

Which will result in two A records one for SD1 and one for SD3. As long as you have at least one recordtype left (this also works for MX, MS, TXT) you will be able to remove others.

If you want to remove the last you have to go back to the Godaddy GUI, unfortunately.

Ray Oei
  • 1,827
  • 1
  • 17
  • 21