azure-devops-node-api

repository·master·Indexed 19 days ago

https://github.com/microsoft/azure-devops-node-api

A Node.js client library for integrating with Azure DevOps and Team Foundation Server (TFS) REST APIs. It provides strongly-typed TypeScript interfaces for services including Build, Git, Work Item Tracking, Release, Test, Wiki, and Core.

Tokens
1.2K
Snippets
6
Records
8
Agent score
18%

What's inside azure-devops-node-api

  1. Create an Azure DevOps connection

    master

    To interact with Azure DevOps, you must first create a connection using a collection URL and an authentication handler. For Personal Access Tokens (PAT), use getPersonalAccessTokenHandler.

    Note on URLs: While most APIs use the organization level URL (e.g., https://dev.azure.com/{yourorgname}), some APIs like ProfileApi require a deployment level URL structured as https://vssps.dev.azure.com/{yourorgname}.

    import * as azdev from "azure-devops-node-api";
    
    // your collection url
    let orgUrl = "https://dev.azure.com/yourorgname";
    
    let token: string = process.env.AZURE_PERSONAL_ACCESS_TOKEN;
    
    let authHandler = azdev.getPersonalAccessTokenHandler(token); 
    let connection = new azdev.WebApi(orgUrl, authHandler);    
  2. Run the library samples

    master

    To run the included samples, ensure you have Node.js >= 16 and TypeScript (tsc) >= 4 installed.

    1. Install dependencies: npm install
    2. Set required environment variables:
      • API_URL: Your Azure DevOps organization URL.
      • API_TOKEN: Your Personal Access Token.
      • API_PROJECT: The name of your project.
    3. Execute samples:
      • All samples: npm run samples
      • Specific sample: npm run samples -- <sampleName> (e.g., npm run samples -- projectAnalysis)
    # Example environment setup and execution
    export API_URL=https://dev.azure.com/yourorgname
    export API_TOKEN=your_token_here
    export API_PROJECT=myProject
    
    npm run samples
  3. Use an API client to perform tasks

    master

    API clients support asynchronous operations using async/await. You can import specific interfaces to ensure type safety when working with returned data structures.

    import * as bi from "azure-devops-node-api/interfaces/BuildInterfaces";
    
    async function run() {
        let project: string = "myProject";
        // 'build' is an instance of IBuildApi obtained from the connection
        let defs: bi.DefinitionReference[] = await build.getDefinitions(project);
    
        defs.forEach((defRef: bi.DefinitionReference) => {
            console.log(`${defRef.name} (${defRef.id})`);
        });    
    }
    
    run();
  4. Get an instance of an API client

    master

    Once a WebApi connection is established, you can retrieve specific API clients (e.g., BuildApi, GitApi) using methods on the connection object. These methods are asynchronous and return the requested client instance.

    import * as ba from "azure-devops-node-api/BuildApi";
    
    let build: ba.IBuildApi = await connection.getBuildApi();
  5. Available API clients

    master

    The library provides access to a wide range of Azure DevOps services through specialized client classes. Common clients include:

    • Build
    • Git
    • Work
    • WorkItemTracking
    • Release
    • Test
    • Wiki
    • Core
    • ...and many others (e.g., Advanced Security, Dashboard, ExtensionManagement, Profile, etc.).
  6. Node.js version support and compatibility

    master

    Node.js Support

    • v14 and above: Current and maintained. Supports Node 16 and above.
    • v13 and below: End of Life. Contains security vulnerabilities; use at your own risk for Node < 16.

    API and TFS Mapping

    Use the following table to match the azure-devops-node-api version with your TFS/Azure DevOps Server version:

    TFS VersionNode API VERSION
    Azure DevOps Server vNext8.0.0
    Azure DevOps Server 20197.0.0
    TFS 2018 Update 26.6.2
    TFS 2017 Update 26.2.8-preview
    TFS 2017 Update 15.1.2
    TFS 2017 RTW5.0.0
    TFS 2015 Update 20.7.0
  7. Initialize the Azure DevOps Node API library

    master

    The library is initialized by importing the main entrypoint. This automatically loads the core WebApi module and necessary extensions required for full functionality. To use the library, import the module as shown in the usage examples.

    import trm = require('./WebApi');
    require('./extensions');