Is there an API call for adding SSH keys to a BitBucket repo?

Is there a REST API or other mechanism that I can use to programmatically add a public key to a specific repository in BitBucket? The end goal would be to add a public key to BitBucket for either a user or a specific repo without having to manually copy and paste the key into the GUI

2 answers

0 vote

Bitbucket Server or Bitbucket Cloud?

0 vote

There is a REST endpoint that you can use:

To add an SSH key to the current user

POST

{
    "text": "ssh-rsa AAAAB3... me@127.0.0.1",
    "label": "me@127.0.0.1"
}

to <bitbucket-baseurl>/rest/ssh/latest/keys

(label is optional)

To add an SSH key to a repository

POST

{
    "key": {
        "text": "ssh-rsa AAAAB3... me@127.0.0.1",
"label": "some-label" }, "permission": "REPO_WRITE" }

to <bitbucket-baseurl>/rest/ssh/latest/projects/<project-key>/repos/<repo-slug>

You can find all SSH endpoints in the Bitbucket Server Docs under the REST documentation for SSH keys under 

Perfect! This is exactly what I needed - thanks very much for sending that over. I'll give it a try and let you know if I have any issues. Thanks again!

Is the documentation website for the REST documentation for SSH keys down? I'm attempting to access it and am getting the below error. I've also included the URL (accessed by clicking the link that you provided) I'm attempting to access should that prove useful.  I also attempted to access the documentation for my version of bitbucket (4.9.1) and also got a 404 but with a different output. If this API and its documentation is substantially different from the current version, can you please provide a link to that as well? Thanks in advance for your time and attention to this matter.

https://docs.atlassian.com/DAC/rest/bitbucket-server/5.6.2/bitbucket-ssh-rest.html

404 Not Found

  • Code: NoSuchKey
  • Message: The specified key does not exist.
  • Key: DAC/rest/bitbucket-server/5.6.2/bitbucket-ssh-rest.html
  • RequestId: B8A857E92F5D8C73
  • HostId: +Be8+F3ofVltMDPPCvB4aZE/xvfvIT+iQSSgWvU70MBReRPOctL08IM1Ae6cUgAcfRvdnRwmJAg=

An Error Occurred While Attempting to Retrieve a Custom Error Document

  • Code: NoSuchKey
  • Message: The specified key does not exist.
  • Key: error.html

Hello

 I am trying to add the key to the repository with REPO_WRITE permission, but it looks like it ignores permission and forcibly add the key with REPO_READ permission.

 Should the user have some special permissions to add the key with  REPO_WRITE permission? 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jun 12, 2018 in Bitbucket

Do you use any Atlassian products for your personal projects?

After spinning my wheels trying to get organized enough to write a book for National Novel Writing Month (NaNoWriMo) I took my affinity for Atlassian products from my work life and decided to tr...

23,554 views 26 12
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you