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:
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 https://api.godaddy.com/v1/domains/mydomain.com/records and the authentication was in the request 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.)
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:
To get a more legible output, I reran the command. This time, piping the output to
After successfully retrieving the DNS records for my domain, I repeated the process, this time using the
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!