Azure DevOps: Creating Personal Access Token (PAT)

Sign in to your organisation (https://dev.azure.com/{yourorganization})

Treat and use a PAT like your password and keep it a secret.

Use your PAT anywhere your user credentials are required for authentication in Azure DevOps.

You can use a personal access token (PAT) as an alternate password to authenticate into Azure DevOps.

A personal access token contains your security credentials for Azure DevOps. A PAT identifies you, your accessible organizations, and scopes of access. As such, they're as critical as passwords, so you should treat them the same way.

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 third-party tools that don't support Microsoft or Azure AD accounts – or you don't want to provide your primary credentials to the tool – use PATs to limit your risk.

find more details about creating PAT in Azure DevOps

Select Organization from Azure DevOps Portal firstly, then create Personal Access Token for it.

I selected kenanhancer organization in the first screenshot above so that I can create Personal Access Token(PAT) for it or all.

When calling any Azure DevOps REST API to make some actions like get, create, delete etc, be sure that you are using correct Azure DevOps Organization Personal Access Token(PAT) otherwise it will error.

Test your Personal Access Token(PAT)

find more details about Azure DevOps in Projects REST API

username is not necessary when you use Personal Access Token(PAT) but don't forget to use : sign before Personal Access Token(PAT).

$ curl -u <your_user_name>:<your_personal_access_token> 'https://dev.azure.com/<your_organization_name>/_apis/projects?api-version=6.0'

we could call this service as below

  • Create a base64 encode token from <your_user_name>:<your_personal_access_token>
  • Pass that base64 encode token to Authorization header in curl command
$ TOKEN=$(echo -n "<your_user_name>:<your_personal_access_token>" | base64)

$ curl --header "Authorization: Basic $TOKEN" 'https://dev.azure.com/<your_organization_name>/_apis/projects?api-version=6.0'

Instead of above usage, we could use the following single line usage.

$ echo -n "<your_user_name>:<your_personal_access_token>" | base64 | read TOKEN; curl --header "Authorization: Basic $TOKEN" 'https://dev.azure.com/<your_organization_name>/_apis/projects?api-version=6.0'

Listing kenanhancer organization's projects

I use real Personal Access Token(PAT) but they are already removed.

Below two commands are doing same job.

$ echo -n ":wcvgweg6mrja3a4dofdrnkqo7cmnn3mmttjlq7w5e2vldcbgfjna" | base64 | read TOKEN; curl --header "Authorization: Basic $TOKEN" 'https://dev.azure.com/kenanhancer/_apis/projects?api-version=6.0'

or

$ curl -u :wcvgweg6mrja3a4dofdrnkqo7cmnn3mmttjlq7w5e2vldcbgfjna 'https://dev.azure.com/kenanhancer/_apis/projects?api-version=6.0' | jq .

{
  "count": 11,
  "value": [
    {
      "id": "d9e0ed53-71d8-4ffb-a443-dab03d52407a",
      "name": "Scala",
      "url": "https://dev.azure.com/kenanhancer/_apis/projects/d9e0ed53-71d8-4ffb-a443-dab03d52407a",
      "state": "wellFormed",
      "revision": 438399487,
      "visibility": "private",
      "lastUpdateTime": "2018-01-16T22:22:34.013Z"
    },
    {
      "id": "5aa34b2f-6aec-47b2-aac4-70b89499b429",
      "name": "C_Projects",
      "url": "https://dev.azure.com/kenanhancer/_apis/projects/5aa34b2f-6aec-47b2-aac4-70b89499b429",
      "state": "wellFormed",
      "revision": 438399397,
      "visibility": "private",
      "lastUpdateTime": "2016-09-23T20:07:25.43Z"
    },
    {
      "id": "d2d0e07d-4f64-4329-b1dc-92603a618141",
      "name": "Nodejs",
      "url": "https://dev.azure.com/kenanhancer/_apis/projects/d2d0e07d-4f64-4329-b1dc-92603a618141",
      "state": "wellFormed",
      "revision": 438399480,
      "visibility": "private",
      "lastUpdateTime": "2018-01-14T00:08:14.21Z"
    },
    {
      "id": "2aa6c974-3209-4dcc-9897-cb7dafadd1a7",
      "name": "Java",
      "url": "https://dev.azure.com/kenanhancer/_apis/projects/2aa6c974-3209-4dcc-9897-cb7dafadd1a7",
      "state": "wellFormed",
      "revision": 438399456,
      "visibility": "private",
      "lastUpdateTime": "2017-08-04T07:23:16.07Z"
    },
    {
      "id": "ecb75760-b80b-465a-bc76-949b9f8e2aa9",
      "name": "Uni",
      "url": "https://dev.azure.com/kenanhancer/_apis/projects/ecb75760-b80b-465a-bc76-949b9f8e2aa9",
      "state": "wellFormed",
      "revision": 438399372,
      "visibility": "private",
      "lastUpdateTime": "2015-08-11T21:55:59.54Z"
    },
    {
      "id": "f8612f75-9006-42be-9808-44609cbc203e",
      "name": "Kenanhancer-github",
      "url": "https://dev.azure.com/kenanhancer/_apis/projects/f8612f75-9006-42be-9808-44609cbc203e",
      "state": "wellFormed",
      "revision": 438399514,
      "visibility": "public",
      "lastUpdateTime": "2022-06-28T14:35:37.147Z"
    },
    {
      "id": "4ae3a11a-cc1e-4a01-b484-b1b8b8c331a5",
      "name": "Python_Projects",
      "url": "https://dev.azure.com/kenanhancer/_apis/projects/4ae3a11a-cc1e-4a01-b484-b1b8b8c331a5",
      "state": "wellFormed",
      "revision": 438399413,
      "visibility": "private",
      "lastUpdateTime": "2016-09-28T18:45:29.467Z"
    },
    {
      "id": "c9975d2c-194d-4942-a4ae-a77d6aa49755",
      "name": "howden-test1",
      "url": "https://dev.azure.com/kenanhancer/_apis/projects/c9975d2c-194d-4942-a4ae-a77d6aa49755",
      "state": "wellFormed",
      "revision": 438399495,
      "visibility": "private",
      "lastUpdateTime": "2022-06-06T12:36:35.973Z"
    },
    {
      "id": "32c117f5-ca3f-4e53-9202-bea1552173c4",
      "name": "Web_Projects",
      "url": "https://dev.azure.com/kenanhancer/_apis/projects/32c117f5-ca3f-4e53-9202-bea1552173c4",
      "state": "wellFormed",
      "revision": 438399427,
      "visibility": "private",
      "lastUpdateTime": "2016-10-12T18:29:56.417Z"
    },
    {
      "id": "a57cf5fa-bf35-4bcf-8703-2b700d0c3c61",
      "name": "CSharp_Projects",
      "url": "https://dev.azure.com/kenanhancer/_apis/projects/a57cf5fa-bf35-4bcf-8703-2b700d0c3c61",
      "state": "wellFormed",
      "revision": 438399420,
      "visibility": "private",
      "lastUpdateTime": "2016-09-28T18:47:30.127Z"
    },
    {
      "id": "8b534055-8f4d-404d-932e-e27e74a5d83d",
      "name": "Haskee",
      "url": "https://dev.azure.com/kenanhancer/_apis/projects/8b534055-8f4d-404d-932e-e27e74a5d83d",
      "state": "wellFormed",
      "revision": 438399473,
      "visibility": "private",
      "lastUpdateTime": "2017-12-26T19:21:46.693Z"
    }
  ]
}
$ curl -u :wcvgweg6mrja3a4dofdrnkqo7cmnn3mmttjlq7w5e2vldcbgfjna 'https://dev.azure.com/kenanhancer/_apis/projects?api-version=6.0' | jq  '.value[].name'

"Scala"
"C_Projects"
"Nodejs"
"Java"
"Uni"
"Kenanhancer-github"
"Python_Projects"
"howden-test1"
"Web_Projects"
"CSharp_Projects"
"Haskee"

Listing kenanhancer2 organization's projects

Notice that url is updated as below.

https://dev.azure.com/kenanhancer2/_apis/projects?api-version=6.0

$ curl -u :ej6ugwtwhaq4cow4zdximlq2yih5oqfm4inxkgeenrojdfew75oq 'https://dev.azure.com/kenanhancer2/_apis/projects?api-version=6.0' | jq .

{
  "count": 4,
  "value": [
    {
      "id": "87c3750b-422d-4c45-84b5-893f8ed84bbd",
      "name": "Test-project1",
      "url": "https://dev.azure.com/kenanhancer2/_apis/projects/87c3750b-422d-4c45-84b5-893f8ed84bbd",
      "state": "wellFormed",
      "revision": 27,
      "visibility": "private",
      "lastUpdateTime": "2022-06-29T11:40:53.307Z"
    },
    {
      "id": "24a7b00f-de42-4428-84e3-40ac69c275ca",
      "name": "Test-project2",
      "url": "https://dev.azure.com/kenanhancer2/_apis/projects/24a7b00f-de42-4428-84e3-40ac69c275ca",
      "state": "wellFormed",
      "revision": 35,
      "visibility": "private",
      "lastUpdateTime": "2022-06-29T11:41:07.1Z"
    },
    {
      "id": "5ab40ef0-b388-4bbf-8812-a985b0c42b58",
      "name": "kenanhancer-bitbucket",
      "url": "https://dev.azure.com/kenanhancer2/_apis/projects/5ab40ef0-b388-4bbf-8812-a985b0c42b58",
      "state": "wellFormed",
      "revision": 19,
      "visibility": "private",
      "lastUpdateTime": "2022-06-29T11:40:17.48Z"
    },
    {
      "id": "a5717866-dfcb-47b2-a195-9e6a447b86a3",
      "name": "Kenanhancer-github",
      "url": "https://dev.azure.com/kenanhancer2/_apis/projects/a5717866-dfcb-47b2-a195-9e6a447b86a3",
      "state": "wellFormed",
      "revision": 11,
      "visibility": "private",
      "lastUpdateTime": "2022-06-29T11:34:47.767Z"
    }
  ]
}
$ curl -u :ej6ugwtwhaq4cow4zdximlq2yih5oqfm4inxkgeenrojdfew75oq 'https://dev.azure.com/kenanhancer2/_apis/projects?api-version=6.0' | jq  '.value[].name'

"Test-project1"
"Test-project2"
"kenanhancer-bitbucket"
"Kenanhancer-github"

Leave a Reply