How do I parse an API call response to trigger a slack notification?

I have an API that when I call it, 99% of the time I get the following response:

{
    "anomaly": ""
}

Every once and a while, the API call returns:

{
    "anomaly": "A string with the useful information here"
}

What I want to do, is to send a Slack notification (through a slack template) whenever the response from the API is != “anomaly”: “”. Also, I want to be able to send the string that comes in the “anomaly” parameter, inside the slack notification. How can I store the value from “anomaly” in a variable so I can perform logic with that?

Thanks!

Hey @nicolas.spencer , thanks for reaching out!

What you’ll most likely want here is to take advantage of output variables and run conditions in Octopus.

From your use case, we’ve seen it modeled most often where your script step that reaches out to the API sets an output variable with the value of the anomaly field. Then, in your Slack notification step, you’ll set a run condition that evaluates the output variable to determine whether the step should run.

Here’s a quick example with some pseudocode to hopefully get you started-

Step 1 - Call API

$apiCallResult = ((Invoke-WebRequest "https://myapi" | ConvertFrom-Json)).anomaly

if (![string]::IsNullOrEmpty($apiCallResult))
{
    Set-OctopusVariable -name "ApiResult" -value $apiCallResult
}

Step 2 - Send Slack Notification - Run Condition
#{if Octopus.Deployment.Error}#{Octopus.Action[Call API].Output.ApiResult}#{/if}

Obviously replace the chunks to match your needs, but in general, here’s what should happen:

  • API call is made
  • If there’s a value in anomaly, set an output variable
  • If there’s no error in the deployment, and there’s a value in the output variable, run the subsequent step

Have a look at that, and let me know how it goes!

1 Like

Hi @cory.reid , thank you for your response. I managed to do something similar base on this pseudo code.
Thank you for your help!
Regards,
Nicolás