Bitbucket code search api is not working
Asked by nasrulrules:

I am trying to use the bitbucket code search API and I am getting 401 error "**Token is invalid or not supported for this endpoint.**"

I created a repository access token and can access this particular URL '<https://api.bitbucket.org/2.0/repositories/nasrulrules>' with an authorization bearer token in the header without any issue.

But I couldn't access the code search API with the same token. I am having an invalid token issue. My code search api -  
**<https://api.bitbucket.org/2.0/teams/nasrulrules/search/code?search_query=repo%3Asample%20source>**  
username - **nasrulrules**  
**<https://bitbucket.org/blog/bitbucket-code-search-api-now-available>**

**<https://developer.atlassian.com/cloud/bitbucket/rest/api-group-other-operations/#api-teams-username-search-code-get>**

I am following the above two articles. In these articles it is mentioned like **'GET /2.0/teams/{username}/search/code**'

what username I should give? I already tried with 'nasrulrules'

In the first article I am seeing the api you provided and what is "**%7B6f461d1e-a3dd-433b-a0e3-7a69daf6ea47%7D" ?**

<https://api.bitbucket.org/2.0/teams/>**%7B6f461d1e-a3dd-433b-a0e3-7a69daf6ea47%7D**/search/code?search_query=repo%3Aelasticsearch%20QueryBuilders

kindly provide more suggestions on this.

**Note:** This is a private repository and I am the only user in it.


Answers:
author: Patrik S
body: Hello [@nasrulrules](/t5/user/viewprofilepage/user-id/5112169) ,

Welcome to Atlassian Community!

The 401 Authorization error usually means that your credentials are either invalid or do not have sufficient permissions to execute that action. If you created the token using an [Oauth consumer](https://support.atlassian.com/bitbucket-cloud/docs/use-oauth-on-bitbucket-cloud/), could you try to check all the scopes when creating the consumer to make sure you have the necessary permissions when doing the API call?

![Screenshot 2023-01-10 at 4.33.50 PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/233779iC581A73EE2842B19/image-dimensions/396x265?v=v2 "Screenshot 2023-01-10 at 4.33.50 PM.png")

As for the endpoint, Bitbucket Cloud API provides two different endpoints for code search, with the difference them being the type of the workspace: personal or shared (team) workspaces.

If you want to Code Search in a **personal** workspace :

* [Search code in a user's repository](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-other-operations/#api-users-selected-user-search-code-get)   

  Example API call :  

  ```
  curl --request GET \

    --url 'https://api.bitbucket.org/2.0/users/<personal workspace id>/search/code?search_query=foo+repo:demo' \

    --header 'Authorization: Bearer <Bearer token>' \

    --header 'Accept: application/json'
  ```

If you want to Code Search on a **shared** workspace :

* [Search code in a team's repository](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-other-operations/#api-teams-username-search-code-get)   

  Example API call :   

  ```
  curl --request GET \

    --url 'https://api.bitbucket.org/2.0/teams/<shared workspace id>/search/code?search_query=foo+repo:demo' \

    --header 'Authorization: Bearer <Bearer token>' \

    --header 'Accept: application/json'
  ```

You're also welcome to check [Difference between shared and personal workspaces](https://confluence.atlassian.com/bbkb/difference-between-shared-and-personal-workspaces-1141477191.html) for more details on each type of workspace.

Hope that helps! Let me know in case you're still running into issues with that endpoint.

Thank you, [@nasrulrules](/t5/user/viewprofilepage/user-id/5112169).

Patrik S

comments: author: Johnny O
body: [@Patrik S](/t5/user/viewprofilepage/user-id/4603309) I am getting the same 401 error. I currently have these scopes enabled.

![Screenshot 2023-03-31 at 09.11.44.png](https://community.atlassian.com/t5/image/serverpage/image-id/249102iF7A90EA9CD887251/image-size/large?v=v2&px=999 "Screenshot 2023-03-31 at 09.11.44.png")

Do you mind sharing the exact scopes needed to enable code search at the workspace level?


author: Patrik S
body: Hello [@Johnny O](/t5/user/viewprofilepage/user-id/5184938) ,

Unfortunately, workspace access tokens are not currently supported in the code search endpoints. I went ahead and raised a feature request for it :

* <https://jira.atlassian.com/browse/BCLOUD-22586>

Feel free to add your vote and mark yourself as a watcher to receive updates whenever there's any progress on the implementation of that feature.

For now, to authenticate requests to the code search endpoints you will need to use the username and [app password](https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/) or [OAuth access token.](https://support.atlassian.com/bitbucket-cloud/docs/use-oauth-on-bitbucket-cloud/#Access-tokens)

Thank you, [@Johnny O](/t5/user/viewprofilepage/user-id/5184938) !

Patrik S


author: Johnny O
body: Thank you for that clarification and opening the feature request. Would updating the documentation to call out that those API endpoints need username and app password or auth access token be worth while?


author: Patrik S
body: Hey [@Johnny O](/t5/user/viewprofilepage/user-id/5184938) ,

I've reached out internally to the team responsible for those API endpoints to get the documentation updated. Thank you for reporting this [@Johnny O](/t5/user/viewprofilepage/user-id/5184938) !

Patrik S


Tags: bitbucket-cloud

cloud