Clint Colding    Archive    Feed    About

Getting Started with API’s in PowerShell

PowerShell is an impressively powerful tool and one of its most powerful features is its ability to call API’s. For those of us that have a primarily infrastructure focused background, we understand what an API is and does, but we’re a little foggy on how to use them.

The first API project I completed used the GoDaddy API. I was frustrated with the tedious process of using the GoDaddy web portal to update DNS records across our various domains. So I decided to create more efficient PowerShell commands.

I started off doing some research, thankfully GoDaddy has wonderful documentation of their API’s. I decided a logical place to start was to return all the records associated with a specific domain. Under the /v1/domains API, I found a Get request that retrieves DNS records for the specified domain:

GoDaddy Get API

The first step of making any API call is finding the API URL and what type of authentication is needed, if any. By expanding the documentation pane above, I found both.

The URL was and the authentication was in the request header:

GoDaddy API Header

Now that I knew what I needed, I could start building my PowerShell script. To make the API call I used Invoke-WebRequest with the following parameters:

  • URI: Specifies the Uniform Resource Identifier (URI) of the Internet resource to which the web request is sent.
  • Method: Specifies the method used for the web request. (Get, Post, Put, etc)
  • Headers: Specifies the headers of the web request. Enter a hash table or dictionary.

First I needed to create a “Authorization” table with my key/secret pair that I could pass into the request header. You can get your API keys here. (Use a production key.)

$apiKey = '2s7Yn1f2dW_W5KJhWbGwuLhyW4Xdvgb2c'
$apiSecret = 'oMmm2m5TwZxrYyXwXZnoN'

$Headers = @{}
$Headers["Authorization"] = 'sso-key ' + $apiKey + ':' + $apiSecret

Remember, we need to recreate the request header that we found in the documentation.

Once I was confident that I’d be able to authenticate, I ran Invoke-WebRequest using the Get method to retrieve all records for my domain. The API successfully returned the data in JSON:

C:\> Invoke-WebRequest -Method Get -Headers $Headers

StatusCode        : 200
StatusDescription : OK
Content           : [{"type":"A","name":"@","data":"","ttl":600},{"type":"A","name":"@","data":"19
RawContent        : HTTP/1.1 200 OK
                    Access-Control-Allow-Credentials: true
                    Vary: Origin,Accept-Encoding
                    x-newrelic-app-data: PxQPUVdRCwcTVlRXDgkOVVATGhE1AwE2QgNWEVlbQFtcCxYkSRFBBxdFXRJJJH1nH0sXUxh
Forms             : {}
Headers           : {[Access-Control-Allow-Credentials, true], [Vary, Origin,Accept-Encoding],
                    [x-newrelic-app-data, PxQPUVdRCwcTVlRXDgkOVVATGhE1AwE2QgNWEVlbQFtcCxYkSRFBBxdFXRJJJH1nH0sXUx
                    5UVQRbDAIAXQRVBgMPREhXV18RAz4=], [Access-Control-Allow-Origin, *]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 696

To get a more legible output, I reran the command. This time, piping the output to ConvertFrom-Json.

C:\> Invoke-WebRequest -Method Get -Headers $Headers | ConvertFrom-Json

type  name           data                                 ttl
----  ----           ----                                 ---
A     @                           600
A     @                           600
CNAME email              3600
CNAME ftp            @                                   3600
CNAME www            @                                   3600
CNAME _domainconnect 3600
MX    @             3600
MX    @                   3600
NS    @                  3600
NS    @                  3600

After successfully retrieving the DNS records for my domain, I repeated the process, this time using the Put method.

In the end, this made updating DNS entries across our domains much more efficient. I also built custom DNS failover scripts for disaster recovery.

I have a GitHub project that includes the basic Get, Add, and Set commands for working with your GoDaddy DNS.

This is just a glimpse of what you can do with GoDaddy’s API. Make sure to check out the documentation, I’d love to see what you automate!