Creating a File in VSTS with APIs

ViewAPIArticles16Following along the same line as some of the previous API examples I’ve posted I thought I’d post an example on how to make use of the VSTS APIs to create a new file in a VSTS-based Git repo. This can be handy if you need to get a file into VSTS but don’t want to mess around with calling out to the Git command line or making use of various Git libraries.

If you’re new to calling the VSTS REST APIs or you are new to this series of articles then I would recommend you check out the other articles in this series before getting started.

For our example (of creating a new file) there are two VSTS APIs that must be utilized: the refs API and the pushes API.

The *refs* API is needed because we need to find the objectId for the branch where the file is going to be created. The objectId will be utilized as an input parameter for the *pushes* API.

Obtaining the Branch Object ID

The format of the API call to obtain the Object ID for the branch where the file will be created looks something like this:


A real-world example of the call might look like this:

In the above example, I am simply attempting to retrieve some information for the master branch. The following C# example shows how you might call the API in code:

var url = $"";
var authorizationHeader = Convert.ToBase64String(


var request = (HttpWebRequest)WebRequest.Create(url);


$"BASIC {authorizationHeader}");
request.ContentType = "application/json";
request.Method = "GET";

using (var response = (HttpWebResponse)request.GetResponse())
    using (var streamToRead = response.GetResponseStream())
        using (var streamReader = new StreamReader(streamToRead,

            var resultsJson = streamReader.ReadToEnd();

When the API returns, the JSON will include information regarding the branch you specified. The JSON will look something like this:

    "value": [{
    "name": "refs/heads/master",
     "objectId": "6cec7a08d43c744bd54a0eafaf7657a665cdbc9b",
     "count": 1

It is the objectId shown in the above JSON that we are interested in. Once you’ve obtained that value, you can move on to the next API.

Creating the File

The API call we will use is based on the *pushes* API and looks something like:


So, using the same account, project and repo as above, the URL would look something like this:

The particular API we are calling utilizes an HTTP POST call and, therefor, also requires a message body to be passed in. The message body has the following format:

   "refUpdates": [
      "name": "refs/heads/master",
        "oldObjectId": "<GET FROM PREVIOUS API RESULTS>"
    "commits": [
        "comment": "Initial commit.",
        "changes": [
            "changeType": "add",
            "item": {
              "path": "/"
            "newContent": {
             "content": "My first file pushed via API!",
               "contentType": "rawtext"

Take notice of the oldObjectId value in the above JSON. Set this value to the Object ID obtained in the previous (refs) call. This identifies the branch where the file is to reside.

Next, ensure the changeType value is set to add. If the file happens to already exist, you can set this value to edit to update the file.

If you want to get a head start on making these calls with C# code (rather than using Fiddler) check out the post Calling VSTS APIs with C#. This post will show you how to make use of an excellent Fiddler extension, Request to Code, that will provide some starter C# code for you from a selected web request. Very cool!

I’ll be posting some larger examples to GitHub in the near future. Once I do that, I’ll come back and update this post with the appropriate links. Enjoy!

2 thoughts on “Creating a File in VSTS with APIs

Comments are closed.