Azure DevOps Python API Client

7.1.0b4 · active · verified Thu Apr 09

The `azure-devops` library provides a Pythonic wrapper around the Azure DevOps REST APIs, enabling programmatic interaction with Azure DevOps Services and Azure DevOps Server. It allows developers to automate tasks, manage resources like work items, repositories, pipelines, and more. The library has a frequent release cadence, often releasing beta versions to align with the latest Azure DevOps API versions, currently focusing on 7.x APIs.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to an Azure DevOps organization using a Personal Access Token (PAT) and retrieve a list of projects. It is recommended to store sensitive information like PATs in environment variables for security.

import os
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
import pprint

# Retrieve organization URL and Personal Access Token from environment variables
organization_url = os.environ.get('AZURE_DEVOPS_ORG_URL', 'https://dev.azure.com/YOUR_ORGANIZATION')
personal_access_token = os.environ.get('AZURE_DEVOPS_PAT', 'YOUR_PERSONAL_ACCESS_TOKEN')

if not organization_url or organization_url == 'https://dev.azure.com/YOUR_ORGANIZATION':
    print("Please set the AZURE_DEVOPS_ORG_URL environment variable or update the placeholder.")
    exit(1)

if not personal_access_token or personal_access_token == 'YOUR_PERSONAL_ACCESS_TOKEN':
    print("Please set the AZURE_DEVOPS_PAT environment variable or update the placeholder.")
    exit(1)

# Create a connection to the organization
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

# Get a client (the 'core' client provides access to projects, teams, etc.)
core_client = connection.clients.get_core_client()

try:
    # Get the first page of projects
    get_projects_response = core_client.get_projects()
    print("Successfully connected to Azure DevOps. Projects:")
    index = 0
    while get_projects_response is not None:
        for project in get_projects_response.value:
            pprint.pprint(f"[{index}] {project.name}")
            index += 1
        if get_projects_response.continuation_token:
            get_projects_response = core_client.get_projects(continuation_token=get_projects_response.continuation_token)
        else:
            get_projects_response = None
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →