title: How to create Build Definitions through VSTS REST API
body: <h3>Situation:</h3>
  
  <p>I'm currently working on automating CI/CD configurations through bitbucket -> vsts -> azure.</p>
  
  <p>My ideal result is to be able to copy paste (or manually enter) my configuration values into their respective console programs and have the applications configure the whole CI/CD ordeal without having to click through all the web interfaces. It's now possible in Bitbucket and Azure, but creating the VSTS CI/CD configurations through REST API is proving to be difficult.</p>
  
  <p><a href="https://i.imgur.com/Li08ory.png" rel="nofollow noreferrer">Azure resources</a> and Bitbucket configurations are currently created through a simple .NET console application that talks to the REST APIs. Basically copy paste (or manually enter) all the values (<a href="https://pastebin.com/GyZSHA19" rel="nofollow noreferrer">azure input values</a> / <a href="https://pastebin.com/deu85K2L" rel="nofollow noreferrer">bitbucket input values</a>)into the console application and it will configure everything within 5 minutes.</p>
  
  <hr>
  
  <h3>Problem:</h3>
  
  <p>Now I face the harder part of trying to automate build configurations and release configurations in VSTS. Microsoft Docs isn't great on the <a href="https://docs.microsoft.com/en-us/vsts/integrate/concepts/dotnet-client-libraries?view=vsts" rel="nofollow noreferrer">documentation of VSTS client libraries</a>.</p>
  
  <p>I'm honestly at a loss for how I can <a href="https://apisof.net/catalog/Microsoft.TeamFoundation.Build.WebApi.BuildDefinition" rel="nofollow noreferrer">create a build definition</a> through the API or Client Library.</p>
  
  <ul>
  <li><p>The <a href="https://apisof.net/catalog/M%3AMicrosoft.TeamFoundation.Build.WebApi.BuildHttpClient.CreateDefinitionAsync(Microsoft.TeamFoundation.Build.WebApi.BuildDefinition,System.Nullable%7BSystem.Int32%7D,System.Nullable%7BSystem.Int32%7D,System.Object,System.Threading.CancellationToken)" rel="nofollow noreferrer">BuildHttpClient</a> has three methods I can work with:</p>
  
  <pre><code>public virtual Task&lt;BuildDefinition&gt; CreateDefinitionAsync(BuildDefinition definition, Guid project, int? definitionToCloneId = null, int? definitionToCloneRevision = null, object userState = null, CancellationToken cancellationToken = default(CancellationToken));   
  public virtual Task&lt;BuildDefinition&gt; CreateDefinitionAsync(BuildDefinition definition, int? definitionToCloneId = null, int? definitionToCloneRevision = null, object userState = null, CancellationToken cancellationToken = default(CancellationToken));    
  public virtual Task&lt;BuildDefinition&gt; CreateDefinitionAsync(BuildDefinition definition, string project, int? definitionToCloneId = null, int? definitionToCloneRevision = null, object userState = null, CancellationToken cancellationToken = default(CancellationToken));
  </code></pre></li>
  <li><p>The <a href="https://apisof.net/catalog/Microsoft.TeamFoundation.Build.WebApi.BuildDefinition" rel="nofollow noreferrer">BuildDefinition</a> has the following properties.</p>
  
  <pre><code>namespace Microsoft.TeamFoundation.Build.WebApi    
  { 
  [DataContract]    
  public class BuildDefinition : BuildDefinitionReference
      {
          public BuildDefinition();
  
          public List&lt;string&gt; Tags { get; }
          public PropertiesCollection Properties { get; }
          public List&lt;RetentionPolicy&gt; RetentionRules { get; }
          public List&lt;Demand&gt; Demands { get; }
          public IDictionary&lt;string, BuildDefinitionVariable&gt; Variables { get; }
          public List&lt;BuildTrigger&gt; Triggers { get; }
          public ProcessParameters ProcessParameters { get; set; }
          public BuildRepository Repository { get; set; }
          public List&lt;BuildOption&gt; Options { get; }
          public List&lt;BuildDefinitionStep&gt; Steps { get; }
          public bool BadgeEnabled { get; set; }
          public int JobTimeoutInMinutes { get; set; }
          public BuildAuthorizationScope JobAuthorizationScope { get; set; }
          public string DropLocation { get; set; }
          public string Description { get; set; }
          public string Comment { get; set; }
          public string BuildNumberFormat { get; set; }
          public Build LatestBuild { get; }
          public Build LatestCompletedBuild { get; }
      }
   }
  </code></pre></li>
  </ul>
  
  <p>As you can see, the most important properties of a build definition are read-only. </p>
  
  <p>How do I go about creating a build definition through the REST API? Are there better alternatives to VSTS that will allow me to do this? </p>
  
  <hr>