Get variable value using API call

Hi

Is there a way to call an external API, then take that value and use it in variable substitutions?

Eg.
Step 1: call external API chad get value, set octopus var “var1” to the returned value
Step 2: deploy a package, and in the var substitution use the value returned to replace value in web.config or app.config

Thanks

Hello,

Thanks for getting in touch.

Yes, you sure can call an API, store the result and then make use of it. One way is to use Output Variables. Here’s what I did to check; I made a twostep project. I called the first step: Get Api Variable, and it ran the following PowerShell script code (you can pick other languages):

$apiResult = Invoke-RestMethod -Uri https://demo.octopus.com/api
Set-OctopusVariable -name "var1" -value $apiResult

I then chose the deploy a package type for step 2 and had it use a package with a config file looked like this:

<config>
   <web.config>#{Octopus.Action[Get Api Variable].Output.var1}</web.config>
</config>

The first thing I used was the variable substitution syntax #{ }, here’s our detailed documentation page on it, with more details than I can remember / copy paste: https://octopus.com/docs/reference/variable-substitution-syntax

The next part of this process is to make sure that config file gets picked up and processed for variable substitution, I chose the Substitute variables in files option which you can activate for your step by going to Configure features near the bottom of the step setup page. It launches a pop-up for extra items you can have as part of your steps.

Note that output variables are grouped under Octopus.Action with their step name, if you’d like more information on that feature here’s the documentation page that covers that: https://octopus.com/docs/deploying-applications/variables/output-variables

Hope that gets you going, let us know if you still have issues.

Regards,
Nick