Muokkaa

Jaa


REST API samples for Azure DevOps

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019

Most samples in this article use personal access tokens (PATs). PATs are a compact example for authentication. There are many other authentication mechanisms available, including Microsoft Authentication Library, OAuth, and Session tokens. For more information to gauge which is best suited for your scenario, see Authentication guidance.

For more information, see Azure DevOps Services REST API Reference and Get started with REST APIs.

Personal access tokens

Authenticate with Azure DevOps when you use the REST APIs or .NET Libraries.

Get started with these samples and create a PAT.

Tip

PATs are like passwords. Keep them secret. Make sure you save them in a secure location once your PAT gets created.

To provide the PAT through an HTTP header, first convert it to a Base64 string. The following example shows how to convert to Base64 using C#. You can provide the resulting string as an HTTP header in the following format:

Authorization: Basic BASE64USERNAME:PATSTRING

REST API

See the following example of getting a list of projects for your organization via REST API.

using System.Net.Http;
using System.Net.Http.Headers;

...

//encode your personal access token                   
string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));

ListOfProjectsResponse.Projects viewModel = null;

//use the httpclient
using (var client = new HttpClient())
{
    client.BaseAddress = new Uri($"https://dev.azure.com/{OrgName}/");  //url of your organization
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials); 

    //connect to the REST endpoint            
    HttpResponseMessage response = client.GetAsync("_apis/projects?stateFilter=All&api-version=1.0").Result;
          
    //check to see if we have a successful response
    if (response.IsSuccessStatusCode)
    {
        //set the viewmodel from the content in the response
        viewModel = response.Content.ReadAsAsync<ListOfProjectsResponse.Projects>().Result;
                
        //var value = response.Content.ReadAsStringAsync().Result;
    }   
}

.NET Client Libraries

Here, we're using two of the .NET Client Libraries. Make sure you reference the following .NET Client Libraries within your .NET project.

See the following example of getting a list of projects for your organization via .NET Client Libraries.

using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Common;

...

//create uri and VssBasicCredential variables
Uri uri = new Uri(url);
VssBasicCredential credentials = new VssBasicCredential("", personalAccessToken);

using (ProjectHttpClient projectHttpClient = new ProjectHttpClient(uri, credentials))
{
    IEnumerable<TeamProjectReference> projects = projectHttpClient.GetProjects().Result;                    
}

FAQs

Q: Where can I get the source code for the code samples?

A: See the https://github.com/Microsoft/vsts-restapi-samplecode.

Q: Where can I find more information on the .NET library?

A: See the overview of client libraries.