How to: Understanding Code Search Rest API on Azure DevOps

Vinicius Moura
3 min readMar 27, 2024

--

This script will decipher the secrets behind this Rest API

I’ve attempted numerous times to use the Azure Code Search API, yet its documentation lacks user-friendliness. Therefore, now that I’ve finally figured out the correct usage, I’ve opted to compose this post!

Firstly, I’ll clarify where the problem lies: the documentation fails to specify where we should input the sub-filters (such as file extension, file name, class, etc.). This issue becomes apparent when using the resource within Azure DevOps. Let’s contrast the usage experiences:

  • Azure DevOps = Here, it’s evident how to incorporate, alongside the search criteria (main filter), the additional subfilters, with the definition of keywords.
  • Code Search Rest API = In the API, we only have a search field added to the designated filters within the method body itself (Project, Repository, Path, Branch).

Here arises the question: where do we include the subfilters? Where do we specify the keywords and their corresponding values?

After conducting numerous tests, I’ve chosen to incorporate these filters directly into the searchText parameter, concatenating a series of keys and values separated by whitespace.

{ "searchText": "resources file:azure-pipelines  ext:yml" }

The API interprets the searchText parameter and searches for sets of keys and values. But here arises another question: where are these keywords documented?

This documentation show all options and keywords taht you can use on searchText parameter

Then, after extensive research and study, I finally succeeded in the use of this API. Happy reading everyone!!!

{
"count": 1,
"results": [
{
"fileName": "azure-pipelines.yml",
"path": "/azure-pipelines.yml",
"matches": {
"content": [
{
"charOffset": 0,
"length": 9,
"line": 0,
"column": 0,
"codeSnippet": null,
"type": "content"
}
],
"fileName": [
{
"charOffset": 0,
"length": -1,
"line": 0,
"column": 0,
"codeSnippet": null,
"type": null
}
]
},
"collection": {
"name": "DefaultCollection"
},
"project": {
"name": "AzureNaPraticaYAML",
"id": "aaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaaaa"
},
"repository": {
"name": "AzureNaPraticaYAML",
"id": "aaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaaaa",
"type": "git"
},
"versions": [
{
"branchName": "master",
"changeId": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
}
],
"contentId": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
}
],
"infoCode": 0,
"facets": {}
}

--

--