How to create a BitBucket repository using BitBucket API?

Simran Kaur Kahlon
Gray Matrix
Published in
3 min readSep 27, 2020
Image Source — blog.axosoft.com

Bitbucket like we all know is a web-based version control repository hosting service owned by Atlassian.

Everything that could be done using the web console is also made available using the Bitbucket REST APIs.

You can get a list of all the API’s here:

https://developer.atlassian.com/bitbucket/api/2/reference/?utm_source=%2Fbitbucket%2Fapi%2F2%2Freference&utm_medium=302

But I wish the documentation was a little clear, I had a hard time understanding.

I explored a couple of things such as:

  • OAuth
  • Creating a Repository

This will surely give you a start as to how to use the other APIs also. So let’s get started.

OAuth 2.0

This is the link that I followed for OAuth,

https://developer.atlassian.com/cloud/bitbucket/oauth-2/?utm_source=%2Fstatic%2Fbitbucket%2Fconcepts%2Foauth2.html&utm_medium=302

Bitbucket provides you around 4 to 5 ways for authentication. I used the Resource Owner Password Credentials Grant (4.3).

It is useful if you have the end user’s password but you want to use a more secure end-user access token instead.

But this method won’t work if you have two-step verification enabled.

curl -X POST -u “client_id:secret” \ https://bitbucket.org/site/oauth2/access_token -d grant_type=password \ -d username={username} -d password={password}

  • username is your bitbucket username
  • password is your bitbucket password

Client_id and secret could be created from the bitbucket console as follow:

  • Go to your Workspace, Settings. In there you will find an option as OAuth Consumers
  • Click on Add Consumer and it will ask you for the below details:
  • The only details you will have to fill in are the Name and Callback URL, and the Permissions you want to set for the new consumer.
  • That’s it, you can fetch the key and secret after creating the consumer and use.

So, let’s run the above curl with the details as:

curl -X POST -u “hgXXX9:yXXXXXXXXXQ” https://bitbucket.org/site/oauth2/access_token -d grant_type=password -d username=XXXX -d password=XXXXX

Its output is as:

{“scopes”: “pipeline:variable webhook wiki snippet:write project:write repository:delete pullrequest:write team:write repository:admin account:write issue:write”, “access_token”: “xxxxxxxxx”, “expires_in”: 7200, “token_type”: “bearer”, “state”: “password”, “refresh_token”: “xxxx”}

The access token that we get can be used in the following ways:

  1. Send it in a request header: Authorization: Bearer {access_token}
  2. Include it in a (application/x-www-form-urlencoded) POST body as access_token={access_token}
  3. Put in the query string of a non-POST: ?access_token={access_token}

Creating a Repository

curl -X POST -H “Content-Type: application/json” -H “Authorization:Bearer XXXXXXXXX” -d ‘{“scm”: “git”,”project”: {“key”: “PROJ”}}’ https://api.bitbucket.org/2.0/repositories/<workspace_name>/<repo_name>

  • workspace_name is the name of your workspace in which you wish to create the new repository
  • repo_name is the name of the new repository to be created.
  • The Access Token that we get in the above OAuth API has to be used in the Authorization Header.
  • The PROJ is the default name for your project. You could use it as-is, also you can change your Project key as below.
  • The Project key can be changed from the Project Settings as:

That’s it, the base is set now and we can go on and use the other bitbucket APIs as well.

Please get in touch in case of any queries.

Thanks.

--

--

Simran Kaur Kahlon
Gray Matrix

JS/ Laravel / AWS / Chatbot Developer #AWS Solution Architect Associate #AWS Developer Associate