0

I want to create work items using nodejs REST API. I have personal access token. Anyone have idea how to authenticate and add new work item to TFS.

Rahul Patil
  • 493
  • 5
  • 14
  • It's not clear what you've tried so far. I would start here: https://learn.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-5.1. And then: https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/create?view=azure-devops-rest-5.1 – Yan Sklyarenko Nov 15 '19 at 07:03
  • A authorization header for PowerShell look like this `$headers = @{ "Authorization" = ('Basic {0}' -f [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)")))}` and the REST API call like `$result = Invoke-RestMethod -Uri $url -Method GET -ContentType $contentType -Headers $headers -Verbose -Timeout 10`. This call is a `GET` if you want to create something you most likly need a `POST` and also a `-Body $body` argument. The `$body` is a json you find in [Work Items](https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/create?) – Mar Tin Nov 15 '19 at 07:28
  • I tried with POST https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${type}?api-version=5.1 this API but it's not working getting 302 now. – Rahul Patil Nov 15 '19 at 09:02
  • What's your tfs version? – PatrickLu-MSFT Nov 15 '19 at 09:52

1 Answers1

1

You could first go through our official link Azure DevOps Client for Node.js, which integrate with Azure DevOps from your Node.js apps.

Since, you already have the PAT token, you could refer below sample code for Create a connection with TFS

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; // e.g "cbdeb34vzyuk5l4gxc4qfczn3lko3avfkfqyb47etahq6axpcqha"; 

let authHandler = azdev.getPersonalAccessTokenHandler(token); 
let connection = new azdev.WebApi(orgUrl, authHandler);  

For how to create a work item, you could also take a look at this similar question: How to create work items using Visual Studio Team Services Client for Node.js (vso-node-api)?

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
  • Thanks for your time. Now I am facing error like "You must pass a valid patch document in the body of the request.". Following data is passed - [{ "op": "add", "path": "/fields/System.Title", "from": null, "value": "Sample bug reporting" }] – Rahul Patil Nov 15 '19 at 12:51