Question

Is it possible to get the ssh key (name or id) of a droplet through api?

Posted February 14, 2020 379 views
DigitalOcean API and CLI (doctl)

Would like to know if we can get the name or id of the ssh key of a droplet through API because while creating droplet from the snapshot I don’t want pass all the keys in my account. Is there any other way to solve it.

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.

×
2 answers

Hello, @vimalSeal

You can check our official documentation of the API via this link:
https://developers.digitalocean.com/documentation/v2/

You can check the SSH Keys part of the page.

id - This is a unique identification number for the key. This can be used to reference a specific SSH key when you wish to embed a key into a Droplet.
fingerprint - This attribute contains the fingerprint value that is generated from the public key. This is a unique identifier that will differentiate it from other keys using a format that SSH recognizes.
public_key - This attribute contains the entire public key string that was uploaded. This is what is embedded into the root user's authorized_keys file if you choose to include this SSH key during Droplet creation.

name - This is the human-readable display name for the given SSH key. This is used to easily identify the SSH keys when they are displayed.

Let me know how it goes.

Regards,
Alex

  • Hi Alex, Thanks for the quick reply. I would like to know if we can get the ssh key name or id that was used in the current droplet , because after taking the snapshot and creating a droplet I need to use that ssh key or id

    • Hello, @vimalSeal

      You can check the example provided in the official docs:

      https://developers.digitalocean.com/documentation/v2/

      curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer b7d03a6947b217efb6f3ec3bd3504582" "https://api.digitalocean.com/v2/account/keys" 
      

      Where you need to replace b7d03a6947b217efb6f3ec3bd3504582 with your actual API token which you’ve received when creating the API access tokens in the control panel.

      The response will be a JSON object with a key set to ssh_keys. The value of this will be an array of key objects, each of which contain the standard key attributes:

      You can also check this blog post:
      https://blog.digitalocean.com/api-now-supports-ssh-keys-for-droplet-creates/

      Hope this helps!

      Regards,
      Alex

      • Hi Alex,
        Is there any way, that we can get which droplet is using which key (If I had more than one key situation)

        Thanks,
        Vimal

        • Hello, @vimalSeal

          A solution will be to name all of your keys to match the droplet name and if you have more than one key added to a droplet you can simply add number to the end of the name or something else that will work for you. In this way when you pull the ssh_key names you can easily determinate which key is attached to the droplet you want.

          Hope this helps!

          Regards,
          Alex

          • Hi ,
            I am looking for a way to get ssh key for the existing i.e currently running droplet. There are chances that the droplet might not be created by me exactly, however, I have to take a snapshot and create a new droplet with it using the existing key which was used.

            Thanks,
            Vimal

Hello, @vimalSeal

Thanks for clearing this out.

I think if you follow up my suggestion this should help you to create the new droplet and to use the ssh_key that was attached/used with the existing droplet in question.

Regards,
Alex

Submit an Answer