Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

How To Make REST API Requests in PowerShell


In this quick guide, we'll walk through the utilities necessary to make an HTTP request to Twilio's API, which is secured with HTTP basic authentication. We go over Invoke-WebRequest and finish by sending an outgoing SMS message.

PowerShell(link takes you to an external page) allows developers to write command line scripts using the full power of the .NET framework. However, some tasks you may be used to performing in *nix environments, like making an authenticated HTTP request with curl, are not as straightforward.


_16
# Pull in Twilio account info, previously set as environment variables
_16
$sid = $env:TWILIO_ACCOUNT_SID
_16
$token = $env:TWILIO_AUTH_TOKEN
_16
$number = $env:TWILIO_NUMBER
_16
_16
# Twilio API endpoint and POST params
_16
$url = "https://api.twilio.com/2010-04-01/Accounts/$sid/Messages.json"
_16
$params = @{ To = "+15558675309"; From = $number; Body = "Hello from PowerShell" }
_16
_16
# Create a credential object for HTTP basic auth
_16
$p = $token | ConvertTo-SecureString -asPlainText -Force
_16
$credential = New-Object System.Management.Automation.PSCredential($sid, $p)
_16
_16
# Make API request, selecting JSON properties from the response
_16
Invoke-WebRequest $url -Method Post -Credential $credential -Body $params -UseBasicParsing |
_16
ConvertFrom-Json | Select sid, body

Let's break down the moving parts of this script.


Export System Environment Variables

export-system-environment-variables page anchor

The first bit you'll need to do is export your Twilio account credentials and phone number, which you'll need to send an outbound SMS. Your account SID and auth token can be found on your Console dashboard, and you can use one of your existing phone numbers (or buy one to use) in the console as well.

There are a number of ways to configure system environment variables on Windows, but you can set user-level environment variables in PowerShell with the following commands. Replace the strings below with the relevant values from your account.


_10
[Environment]::SetEnvironmentVariable("TWILIO_ACCOUNT_SID", "your_account_sid", "User")
_10
[Environment]::SetEnvironmentVariable("TWILIO_AUTH_TOKEN", "your_auth_token", "User")
_10
[Environment]::SetEnvironmentVariable("TWILIO_NUMBER", "+16518675309", "User")

In the example script at the beginning of the article, we access these variables through the $env variable.


_10
# Pull in Twilio account info, previously set as environment variables
_10
$sid = $env:TWILIO_ACCOUNT_SID
_10
$token = $env:TWILIO_AUTH_TOKEN
_10
$number = $env:TWILIO_NUMBER


Configure REST API Endpoint and Credentials

configure-rest-api-endpoint-and-credentials page anchor

After exporting our Twilio account info, we need to configure a URL for the API endpoint we want to hit.


_10
$url = "https://api.twilio.com/2010-04-01/Accounts/$sid/Messages.json"

We substitute a variable that holds our account SID, which is also necessary for the API URL. We append the .json extension to the URL to get the response back in JSON format.

Next, we create a hash table(link takes you to an external page) with the POST parameters we need to send with our request.


_10
$params = @{ To = "+15558675309"; From = $number; Body = "Hello from PowerShell" }

What are those parameters for?

  • To : The phone number you want to send the SMS to
  • From : A Twilio-powered phone number the SMS message will come from
  • Body : The actual text of the outbound message

Now comes a slightly wonky bit we need to do to pass our HTTP basic auth credentials to Twilio in this API request. We need to create a PSCredential(link takes you to an external page) to pass into the Invoke-WebRequest command.


_10
$p = $token | ConvertTo-SecureString -asPlainText -Force
_10
$credential = New-Object System.Management.Automation.PSCredential($sid, $p)


Make an Authenticated API Request in PowerShell

make-an-authenticated-api-request-in-powershell page anchor

Now that we have all our configuration ready, we use the Invoke-WebRequest(link takes you to an external page) command to actually send the SMS. Don't forget the -UseBasicParsing option to prevent creating a DOM from the results, and to avoid errors on systems without Internet Explorer installed (server core, and Windows 10 systems only running Edge browsers).

We also use the ConvertFrom-Json(link takes you to an external page) command to parse the JSON response from the Twilio API, and Select(link takes you to an external page) command to extract properties from the resulting object.


_10
Invoke-WebRequest $url -Method Post -Credential $credential -Body $params -UseBasicParsing |
_10
ConvertFrom-Json | Select sid, body

And there we have it! Let's run that whole thing back one more time.


_16
# Pull in Twilio account info, previously set as environment variables
_16
$sid = $env:TWILIO_ACCOUNT_SID
_16
$token = $env:TWILIO_AUTH_TOKEN
_16
$number = $env:TWILIO_NUMBER
_16
_16
# Twilio API endpoint and POST params
_16
$url = "https://api.twilio.com/2010-04-01/Accounts/$sid/Messages.json"
_16
$params = @{ To = "+15558675309"; From = $number; Body = "Hello from PowerShell" }
_16
_16
# Create a credential object for HTTP basic auth
_16
$p = $token | ConvertTo-SecureString -asPlainText -Force
_16
$credential = New-Object System.Management.Automation.PSCredential($sid, $p)
_16
_16
# Make API request, selecting JSON properties from the response
_16
Invoke-WebRequest $url -Method Post -Credential $credential -Body $params -UseBasicParsing |
_16
ConvertFrom-Json | Select sid, body


PowerShell Text Messages, Oh My

powershell-text-messages-oh-my page anchor

With this code, we can make a REST API request, authenticated with HTTP basic, from PowerShell scripts. If you've run it, you've seen the interesting conclusion - a nice outgoing text message without leaving the comfort of PS.

Want to try something more complex? Try Twilio's Documentation landing page to see our extensive collection of guides, tutorials and quickstarts.


Rate this page: