Get started sample

Personal Access Tokens

When using the REST APIs or .NET Libraries, you need to authenticate with Visual Studio Team Services (VSTS). 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


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

ListofProjectsResponse.Projects viewModel = null;

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

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


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

A: See the

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

A: Yes, see the overview of client libraries