Authenticating with personal access tokens

If you are working on a larger application or project we recommend you check out our authentication guidance page to help you choose the correct authentication mechanism. For smaller projects that require a less robust solution, personal access tokens are a simple alternative. Please be aware that unless your users are using a credential manager (hyper link to below), they will have to enter their credentials each time.

These APIs support OAuth for authorization and you should plan to use that. With Oauth your users don't have to provide their VSTS credentials to use when the APIs are called. To get started on your app, though, you can authenticate using personal access tokens.

Create personal access tokens to authenticate access

  1. Sign in to either your VSTS account (https://{youraccount}.visualstudio.com) or your Team Foundation Server web portal (https://{server}:8080/tfs/).

  2. From your home page, open your profile. Go to your security details.

    TFS 2017

    Go to VSTS account home, open your profile, go to Security

    VSTS

    TFS home page, open your profile, go to Security

  3. Create a personal access token.

    Add a personal access token

  4. Name your token. Select a lifespan for your token.

    If you're using VSTS, and you have more than one account, you can also select the VSTS account where you want to use the token.

    Name your token, select a lifespan. If using VSTS, select an account for your token

  5. Select the scopes that this token will authorize for your specific tasks.

    For example, to create a token to enable a build and release agent to authenticate to VSTS or TFS, limit your token's scope to Agent Pools (read, manage).

  6. When you're done, make sure to copy the token. You'll use this token as your password.

    Use token as the password for your git tools or apps

    Note: Remember that this token is your identity and acts as you when it's used. Keep your tokens secret and treat them like your password.

    Tip: To keep your token more secure, use credential managers so that you don't have to enter your credentials every time. Here are some recommended credential managers:

Revoke personal access tokens to remove access

When you don't need your token anymore, just revoke it to remove access.

  1. From your home page, open your profile. Go to your security details.

    VSTS

    Go to VSTS account home page, open your profile, go to Security

    TFS 2017

    Go to TFS home page, open your profile, go to Security

  2. Revoke access.

    Revoke a token or all tokens

Here's a sample that gets a list of builds using curl.

curl -u username[:{personalaccesstoken}] https://{account}.visualstudio.com/DefaultCollection/_apis/build-release/builds


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 it is in C# using the HttpClient class.

public static async void GetBuilds()
{
    try
    {
        var personalaccesstoken = "PATFROMWEB";

        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Add(
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(
                    System.Text.ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", "", personalaccesstoken))));

            using (HttpResponseMessage response = client.GetAsync(
                        "https://{account}.visualstudio.com/DefaultCollection/_apis/build-release/builds").Result)
            {
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}


When your code is working, it's a good time to switch from basic auth to OAuth.

Enabling IIS Basic Authentication invalidates using PATs for TFS

Learn more about using IIS Basic Authentication with TFS on-premises.

Q&A

Q: Can I use basic auth with all of the VSTS REST APIs?

A: No. You can use basic auth with most of them, but accounts and profiles only support OAuth.