Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to Commit Changes and Delete Files with Repository Access Tokens via REST API?

Mustafa Khaled
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 27, 2024

Hi everyone,

I'm trying to automate some Bitbucket tasks using the REST API and repository access tokens, but I'm having some issues.

Use Case:

I am building a documentation project that will store files on Bitbucket, I need scoped access tokens that will have no access to any other repo on the same workspace. with zero user interactions, because I will not use Bitbucket Oauth, I will be using Google's

What I'm trying to do:

  1. Commit Changes: I want to be able to programmatically create or update files in my repository using the token, but I'm not sure how to structure the requests or what endpoints to use.

  2. Delete Files: I also need to be able to delete files remotely using the API and the token.

What I've tried:

  • I've created a repository access token with the repository:write permission.
  • I've looked through the Bitbucket API documentation, but the examples mostly use personal access tokens or OAuth. ( source endpoints ) 

My Questions:

  • Can someone provide specific examples (preferably in curl) of how to use a repository access token to:
    • Commit new files to a repository
    • Update existing files
    • Delete files

Any help or guidance would be greatly appreciated!

1 answer

1 accepted

0 votes
Answer accepted
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 1, 2024

Hi Mustafa,

You can provide the repository access token in the Authorization header as a bearer token when making requests to Bitbucket APIs, similar to an OAuth access token.

This is an example of committing a new file to the repo and also updating an existing file:

curl https://api.bitbucket.org/2.0/repositories/workspace-id/repo-slug/src \
-F fileA.xml=@fileA.xml \
-F message="some commit message" \
--header 'Authorization: Bearer token_here'

If the file exists in the repo, its content will be replaced with the content of the file you are uploading.

This is an example of committing a file's deletion:

curl https://api.bitbucket.org/2.0/repositories/workspace-id/repo-slug/src \
-F files=fileA.xml \
-F message="some commit message" \
--header 'Authorization: Bearer token_here'

You need to replace workspace-id, repo-slug, fileA.xml, some commit message and token_here with the respective values for your calls.

These calls will create the commits on the repo's main branch. If you want to specify a different branch, you can use -F branch=test-branch in your calls, where test-branch replace with the name of the branch.

Please feel free to reach out if you have any questions!

Kind regards,
Theodora

Mustafa Khaled
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 2, 2024

Thank you bro , it did work 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events