When creating a droplet via the API, you can pass in an SSH key.
What is the format of the key? I am guessing it would be like this, based on return value of the List All Keys API:
{“ssh_keys”:[{“id”:263654,“fingerprint”:“11:37:44:86:62:15:86:5f:0c:7d:41:0b:39:cb:3c:44”,“public_key”:“ssh-rsa ZZZB3NzaC1yc2EAAAADAQABAAABAQC6vHxLRqVEN2Ze9f8ii634wP5f0Ysua+zqcedJFy8D6/biGE2b0pr0fZoB0lyypu3d6uxt8DvxNNRSgcDiZ6JUoTqPWVY252xgaykghBuNsTSFboAT+c/J9/8GQnTJx5PbiUAMeOBsQ6bU3rkhidhsxDyUzp8PyvTHZxopuCZ/QD/qTRgnpXLiV5/RiKMfg69dRLpG9nTWAfXKb4cwBQesPvfSzPemyMWorkLRbnGlDH3s81yIRNDTO9/LKMIPov715lXF4njbvazSnqjLMLNW4MTvSpUt6LFBv4gKCHgqK0V68QV4Py6BDWhpI0nyzZRlnAxKThbJ3nGDThc4d8m5x tom@tomsmacbook.local”,“name”:“For tomsmacbook”}],“links”:{},“meta”:{“total”:1}}
I am still a little fuzzy about SSH and so am not sure exactly what SSH key to pass in. I am guessing I want it to be the public key file (named id_rsa.pub by default) that was created at the same time as the private key my machine uses?
Thanks much.
P.S. Key contents, machine IDs, etc. were changed to protect the innocent.
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
This works for me:
curl -X GET "https://api.digitalocean.com/v2/account/keys" -H "Authorization: Bearer $DO_API_TOKEN"
It works for me when use the following;
curl -X POST “https://api.digitalocean.com/v2/droplets” -d’{“name”:“server1.example.com”,“region”:“nyc3”,“size”:“512mb”,“image”:“centos-6-5-x64”,“ssh_keys”:[“95:0d:c5:a9::xxxx:xxxx:f7:fa”]}’ -H “Authorization: Bearer $TOKEN” -H “Content-Type: application/json”
$TOKEN is my API key
hi, instead of the actual key you have to send the ID of the key.
hi, instead of the actual key you have to send the ID of the key.
hi, instead of the actual key you have to send the ID of the key.
Hello, @tomcampbell
In order to create a droplet using the API you can use the following curl example:
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer b7d03a6947b217efb6f3ec3bd3504582" -d '{"name":"example.com","region":"nyc3","size":"s-1vcpu-1gb","image":"ubuntu-16-04-x64","ssh_keys":[107149],"backups":false,"ipv6":true,"user_data":null,"private_networking":null,"volumes": null,"tags":["web"]}' "https://api.digitalocean.com/v2/droplets"
where ssh_keys is an array containing the IDs or fingerprints of the SSH keys that you wish to embed in the Droplet’s root account upon creation.
You can check our documentation about the API here: https://developers.digitalocean.com/documentation/v2/#create-a-new-droplet
Hope that this helps! Regards, Alex
I’m trying to add the key to the root user of the droplet I am creating. In fact I’m trying to add half a dozen fingerprints to each droplet for our team.
The goal is to avoid the onerous setup of having to log into each droplet and change the password from the one that was auto generated.
The answer below seems to be related to the digital ocean account ssh key. The keys I want to add are from my group’s public keys on off their laptops.
I feel confused. Which is which. Can you explain how I ssh into a droplet with my self generated key and how I format multiple keys into the root user with the api. Not all users who ssh into our droplets will have a digital ocean account.
From the V2 docs re: create new droplet: ssh_keys - Array - An array containing the IDs or fingerprints of the SSH keys that you wish to embed in the Droplet’s root account upon creation.
Thanks, Chris.
So
I believe you want to use the fingerprint value (e.g. 11:37:44:86:62:15:86:5f:0c:7d:41:0b:39:cb:3c:44) not the actual public key.
Latest version, still failing:
And the output is:
This comment has been deleted
Still not working, but I think it should probably be closer to this (TOKEN is a valid token defined in my environment):
Output is:
This exact command but with “ssh_keys”:null works perfectly, creating a droplet.