Following 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.
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 = $"https://MyAccount.visualstudio.com/DefaultCollection/MyProject/_apis/git/repositories/MyRepo/refs/heads/master?api-version=1.0";
var token = "<INSERT YOUR PERSONAL ACCESS TOKEN HERE>";
var authorizationHeader = Convert.ToBase64String(
var request = (HttpWebRequest)WebRequest.Create(url);
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:
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:
"oldObjectId": "<GET FROM PREVIOUS API RESULTS>"
"comment": "Initial commit.",
"content": "My first file pushed via API!",
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!