Generate API key from command line

Hello,
I want to create enviroments via the command line.
I can create the environments with octo.exe but I need an API key. So far the only way I’ve found to generate one is through the web console.
Is there a different method I can use to generate an API key without use for the web console?

According to the documentation Octopus.client does not need an API key.
However the octo.exe command line tool does not work simply with the the username and password like the client.

Is it possible to add it directly into RavenDB using the command line?

Hi Questionmorc,

Like you mentioned, Octo.exe works only with an API Key. May i ask why you dont want to use the web application to create the API Key? It is a once-only procedure that will get you an API key that will work overtime. I case you want to do it from the Web App, here is our documentation on how to do so: http://docs.octopusdeploy.com/display/OD/How+to+create+an+API+key

There is another way yo create an API Key using the Octopus library and Powershell (you can do it on C# as well), which requires a bit more effort:

You need to have the following libraries on the machine running the script:

Octopus.client.dll & Octopus.platform.dll. You can get them both from https://www.nuget.org/packages/Octopus.Client/ or from your Octopus Server at [Octopus Install path]\Octopus or from one of your Octopus Tentacles at [Octopus install path]\Tentacle

Newtonsoft.Json.dll which you can get from https://www.nuget.org/packages/Newtonsoft.Json/ or from your Octopus Server at [Octopus Install path]\Octopus or from one of your Octopus Tentacles at [Octopus install path]\Tentacle

Once you have the 3 libraries, you can use the following Powershell script to generate an API key for a user. Keep in mind though that you will need to put your password in clear text in it to make it work

#Adding libraries. Make sure to modify these paths acording to your environment setup.
Add-Type -Path "C:\Test\Newtonsoft.Json.dll"
Add-Type -Path "C:\Test\Octopus.Client.dll"
Add-Type -Path "C:\Test\Octopus.Platform.dll"
 
#Connection variables
$OctopusURI = 'http://bsas-web-01.cloudapp.net'
 
#Creating a connection
$endpoint = new-object Octopus.Client.OctopusServerEndpoint $OctopusURI
$repository = new-object Octopus.Client.OctopusRepository $endpoint

$LoginObj = New-Object Octopus.Client.Model.LoginCommand 

#Login with credentials.
$LoginObj.Username = "YourUsername"
$LoginObj.Password = "YourPassword"

$repository.Users.SignIn($LoginObj)

$UserObj = $repository.Users.GetCurrent()

$ApiObj = $repository.Users.CreateApiKey($UserObj, "Purpose of your API key")

#Returns the API Key in clear text
$ApiObj.ApiKey

Keep in mind that you’ll only be able to see the API the first time you run the script. So make sure to write it down.

Dalmiro.

This is perfect thank you.

I don’t want to use the web console because I’m developing a chef cookbook to provision machines with octopus installed and configured.

Thanks for the information, has this functionality been removed in Version 3, the Octopus.Platform.dll no longer exists

Hi Stuart,

That script Actually works perfect in V3 if you just remove the line that loads Octopus.platform (which is no longer needed).

I revamped the script to make it prettier and pushed it to our scripts repo. Please use the one on the link below

Hope that helps,
Dalmiro

Hi Dalmiro,
Many thanks for the quick response, looks brilliant and works perfectly. Thanks for all your help.
Cheers