Use personal access tokens

Azure DevOps Services | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018 - TFS 2017

A personal access token (PAT) is used as an alternate password to authenticate into Azure DevOps. In this article, learn how to create, use, modify, and revoke PATs for Azure DevOps.

If you're working within Microsoft tools, then your Microsoft account (MSA) or Azure Active Directory (Azure AD) is an acceptable and well-supported approach. But, if you're working with 3rd party tools that don't support Microsoft or Azure AD accounts – or you don't want to provide your primary credentials to the tool – you can make use of PATs to limit your risk.

PATs are easy to create when you need them and easy to revoke when you don’t. To set up PATs for non-Microsoft tools, use Git credential managers or create them manually. We recommend that you review our authentication guidance to help you choose the correct authentication mechanism. For smaller projects that require a less robust solution, PATs are a simple alternative. Unless your users are using a credential manager, they have to enter their credentials each time.

Create a PAT

Note

To enable the new user interface for the New account manager page, see Manage or enable features.

  1. Sign in to your organization in Azure DevOps (https://dev.azure.com/{yourorganization})

  2. From your home page, open your user settings, and then select Profile.

    My profile Team Services

  3. Under Security, select Personal access tokens, and then select + New Token.

    Select New Token to create

  4. Name your token, select the organization where you want to use the token, and then choose a lifespan for your token.

    Enter basic token information

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

    For example, to create a token to enable a build and release agent to authenticate to Azure DevOps Services, limit your token's scope to Agent Pools (Read & manage). To read audit log events, and manage and delete streams, select Read Audit Log, and then select Create.

    Select scopes for your PAT

  6. When you're done, make sure to copy the token. For your security, it won't be shown again. Use this token as your password.

    Copy the token to your clipboard

  1. Sign in to your Team Foundation Server web portal (https://{server}:8080/tfs/).

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

    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 have more than one organization, you can also select the organization where you want to use the token.

    Name your token, select a lifespan. If using Azure DevOps Services, select an account for your token

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

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

  6. When you're done, make sure to copy the token. For your security, it won't be shown again. Use this token as your password. Select Close.

    Use a token as the password for your Git tools or apps


Once your PAT is created, you can use it anywhere your user credentials are required for authentication in Azure DevOps.

Notifications

Users receive two notifications during the lifetime of a PAT - one upon creation and the other seven days before the expiration.

After you create a PAT, you receive a notification similar to the following example.

PAT created notification

Seven days before your PAT expires, you receive a notification similar to the following example.

PAT near expiration notification

Unexpected notification

If you receive an unexpected PAT notification, an administrator or tool might have created a PAT on your behalf. See the following examples.

  • When you connect to an Azure DevOps Git repo through git.exe. it creates a token with a display name like "git: https://MyOrganization.visualstudio.com/ on MyMachine."
  • When you or an administrator sets up an Azure App Service web app deployment, it creates a token with a display name like "Service Hooks: : Azure App Service: : Deploy web app."
  • When you or an administrator sets up web load testing, as part of a pipeline, it creates a token with a display name like "WebAppLoadTestCDIntToken".
  • When a Microsoft Teams Integration Messaging Extension is set up, it creates a token with a display name like "Microsoft Teams Integration".

If you believe that a PAT exists in error, we suggest that you revoke the PAT. Then, change your password. As an Azure AD user, check with your administrator to see if your organization was used from an unknown source or location.

Use a PAT

Your token is your identity and represents you when it's used. Treat and use a PAT like your password.

  1. Git interactions require a username, which can be anything except the empty string. The PAT is used as the password. Additionally, you have to Base64-encode the username and PAT to use it with HTTP basic authentication. On Linux or macOS, in Bash, you can enter:
MY_PAT=yourPAT		# replace "yourPAT" with your actual PAT
B64_PAT=$(echo ":$MY_PAT" | base64)
git -c http.extraHeader="Authorization: Basic ${B64_PAT}" clone https://dev.azure.com/yourOrgName/yourProjectName/_git/yourRepoName

On Windows, you can do something similar in PowerShell:

$MyPat = 'yourPAT'
$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$MyPat"))
git -c http.extraHeader="Authorization: Basic $B64Pat" clone https://dev.azure.com/yourOrgName/yourProjectName/_git/yourRepoName

To keep your token more secure, use credential managers so you don't have to enter your credentials every time. We recommend the following credential managers:

Use a PAT in your code

See the following sample that gets a list of builds using curl.

curl -u username[:{personalaccesstoken}] https://dev.azure.com/{organization}/_apis/build-release/builds

If you wish 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#). The resulting string can then be provided as an HTTP header in the following format:
Authorization: Basic BASE64_USERNAME_PAT_STRING
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://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.0").Result)
            {
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

Tip

When you're using variables, add a "$" at the beginning of the string, like in the following example.

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://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.0").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.

If you enable IIS Basic Authentication for TFS, PATs aren't valid. For more information, see Using IIS Basic Authentication with TFS on-premises.

For more examples of how to use PATs, see Git credential managers, REST APIs, NuGet on a Mac, [Reporting clients, or Get started with Azure DevOps CLI.

Modify a PAT

You can regenerate or extend a PAT, and modify its scope.

Note

To enable the new user interface for the New account manager page, see Manage or enable features.

  1. From your home page, open your user settings, and then select Profile.

    My profile Team Services

  2. Under Security, select Personal access tokens. Select the token for which you want to modify, and then select Edit.

    Select Edit to modify PAT

  3. Edit the token name, organization it applies to, token expiration, or the scope of access that's associated with the token, and then select Save.

    Modify and Save PAT

Revoke a PAT

You can revoke a PAT at any time, for various reasons.

Note

To enable the new user interface for the New account manager page, see Manage or enable features.

  1. From your home page, open your user settings, and then select Profile.

    My profile Team Services

  2. Under Security, select Personal access tokens. Select the token for which you want to revoke access, and then select Revoke.

    Revoke a token or all tokens

  3. Select Revoke in the confirmation dialog.

    Confirm revoke

Frequently asked questions (FAQs)

Q: Is there a way to renew a PAT via REST API?

A: No, we don't have a REST API to renew a PAT. You can only regenerate a PAT within the user interface (UI).

Q: Can I use basic auth with all of Azure DevOps REST APIs?

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