Get an API key to query octopus API in a step template

Hello.
I need to be able to query Octopus API in a step template with a query like:

curl -s ‘https://OCTOPUS_SERVER/api/SPACE/tasks?project=PROJECT’ -H “X-Octopus-ApiKey: XXXXX” | jq “.Items[] | select(.Description==”$DESCRIPTION") | .State"

I’ve generated one API key and used that locally in a test and that worked but now I need to be able to find a way to access my API key in the step template.

I’ve tried to define a parameter on the step template and used: #{Octopus.Account.Token}.
This doesn’t seem to work.

Any idea how to get a API Token in a step template so that I can query the API?

If there is a better approach to fail a deployment if another deploye failed with some step template that can also be useful but after some investigation I wasn’t able to find a way to do that.

Any help is appreciated.
Thanks.

Hi Petru,

I think I understand what you are aiming for and I have a solution that should make sense for your setup.

We don’t offer a way to address your API token directly. This is for mainly security reasons. If someone was able to run a project, with a link to someone else API key, it could result in all sorts of issue.

Essentially, what you will need to do is set your APIKey as a variable that is accessible by the project.

You could enter the variable in, directly into the project variables - however working with your step template, there is a way that is a little more reusable and secure.

You have no doubt configured your Step Template correctly. Here’s a screenshot of the step-template I’ve created, replacing the apiKey variable. I’ve used the variable “APIKeyToUse” - it should look something like this:

Next, for clarity and reusablity, you should consider a Variable set. This is especially handy if this key will be used in multiple locations along with complementing variables.

If you create a Variable Set, i.e. “Access Token Variable Set”, create a senstive variable inside of the Variable Set called “ActiveAPIKey” and fill it in with your api-key. I have created a plain text variable in this case, just for the example:

You can then link the variable set to the project, that is running the Step Template.


When filling in the Parameter value in the Project Step, set it to #{ActiveAPIKey}.

Finally, when configuring the Linked Step template within the project, bind the parameter “ApiKeyToUse” to the variable #{ActiveAPIKey}

As you can see from the logs, the API-Key gets calculated.

03:04:36   Error    |       At C:\Octopus\Tentacle\Work\20210121030423-16727-3\Script.ps1:1 char:116
03:04:36   Error    |       + ... PROJECT' -H "X-Octopus-ApiKey: API-12345678900000000000"

I hope this makes sense, but please reach out if you get stuck.

Regards,

Dane

1 Like

Hello Dane,

This seems to be what I need to inject the API_KEY into my step template and it seems to work just fine.

Thanks for your help with this,
Petru

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.