Test access to 3rd party resources from a Sitecore PaaS instance

Given how extensible Sitecore is, it’s very common to integrate Sitecore with 3rd party services such as RESTful APIs. Over the years, I’ve worked on many Sitecore implementations that integrated with RESTful APIs implemented by client developers as well as other companies. When working through such integrations, some of the common challenges I’ve faced are networking related. In my experience, 3rd party RESTful APIs are implemented to give consumers access to sensitive data. This being the case, the APIs themselves are protected behind logins and/or network security.

On your local environment, you could confirm you have access a given API by simply pinging it or using PowerShell Invoke-WebRequest. The problem is, neither of these work in a Sitecore PaaS instance.

So to help confirm that a Sitecore PaaS instance has access to a given API, I’ve used this script (which I found here) inside the Sitecore PSE ISE:

# First we create the request.
$HTTP_Request = [System.Net.WebRequest]::Create('https://third-party-api.com/')

# We then get a response from the site.
$HTTP_Response = $HTTP_Request.GetResponse()

# We then get the HTTP code as an integer.
$HTTP_Status = [int]$HTTP_Response.StatusCode

If ($HTTP_Status -eq 200) {
    Write-Host "Site is OK!"
}
Else {
    Write-Host "The Site may be down, please check!"
}

# Finally, we clean up the http request by closing it.
If ($HTTP_Response -eq $null) { } 
Else { $HTTP_Response.Close() }

If the URL you are testing is accessible, you’ll get “Site is OK!”. Otherwise, you’ll get “The Site may be down, please check!”.

If you have other ways of testing access to 3rd party API’s from within a Sitecore PaaS instance, please share in the comments. Also, let me know if you see any gaps in this approach.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s