Get started sample

Last Update: 6/30/2017

Personal Access Tokens

When using the REST APIs or .Net Libraries, you need to authenticate with Team Services. Most samples on this site use Personal Access Tokens as they're a compact example for authenticating with the service. However, there are a variety of authentication mechanisms available for VSTS including ADAL, OAuth and Session Tokens. Refer to the Authentication section for guidance on which one is best suited for your scenario.

To get started with these samples create a personal access token.

Tip: Personal access tokens are like passwords. Keep them secret. Make sure you save them in a secure location once your personal access token is created.

If you wish to provide the personal access token through an HTTP header, you must first convert it to a Base64 string (the following example shows how to convert to Base64 using C#). The resulting string can then be provided as an HTTP header in the format:

Authorization: Basic BASE64PATSTRING

REST API

Here is an example getting a list of projects for your account.

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)));

//create a viewmodel object that is a class that represents 
//the content in the returned json response
ListofProjectsResponse.Projects viewModel = new ListofProjectsResponse.Projects();

//use the httpclient
using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("https://accountname.visualstudio.com:");  //url of our account
    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 succesfull respond
    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

In this example we are using two of the .Net Client Libraries. Make sure these are referenced within your .net project.

Microsoft Team Foundation Server Client

Microsoft Visual Studio Services Client

Here is a simple example getting a list of projects for your account.

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;                    
}

Work item tracking

You can find the following samples and more in C# and .NET at the work item tracking samples page.

Q&A

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 inforation on the .NET library?

A: Yes, see the overview of client libraries

Q: Where can I learn about the specific .Net client libraries contracts?

A: Review the contracts index to learn about the specific .Net client library contracts.