VBScript Xero: Create an Invoice

Back to Index

Example of a draft sales ( ACCREC) invoice with enough detail to be approved once it's been created.

Documentation: https://developer.xero.com/documentation/api/invoices


Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("output.txt", True)

set rest = CreateObject("Chilkat_9_5_0.Rest")

set oauth1 = CreateObject("Chilkat_9_5_0.OAuth1")
oauth1.ConsumerKey = "OAUTH1_CONSUMER_KEY"
oauth1.ConsumerSecret = "OAUTH1_CONSUMER_SECRET"
oauth1.Token = "OAUTH1_TOKEN"
oauth1.TokenSecret = "OAUTH1_TOKEN_SECRET"
oauth1.SignatureMethod = "RSA-SHA1"
set rsaKey = CreateObject("Chilkat_9_5_0.PrivateKey")
'  Insert code here to load the RSA private key...
'  ...
'  then call SetRsaKey to provide the RSA key to the OAuth1 class.
success = oauth1.SetRsaKey(rsaKey)
success = rest.SetAuthOAuth1(oauth1,0)

success = rest.Connect("api.xero.com",443,1,1)
If (success <> 1) Then
    outFile.WriteLine(rest.LastErrorText)
    WScript.Quit
End If

'  The following code creates the JSON request body.
'  The JSON created by this code is shown below.
set jsonReq = CreateObject("Chilkat_9_5_0.JsonObject")
success = jsonReq.UpdateString("Type","ACCREC")
success = jsonReq.UpdateString("Contact.ContactID","eaa28f49-6028-4b6e-bb12-d8f6278073fc")
success = jsonReq.UpdateString("Date","/Date(1518685950940+0000)/")
success = jsonReq.UpdateString("DueDate","/Date(1518685950940+0000)/")
success = jsonReq.UpdateString("DateString","2009-05-27T00:00:00")
success = jsonReq.UpdateString("DueDateString","2009-06-06T00:00:00")
success = jsonReq.UpdateString("LineAmountTypes","Exclusive")
success = jsonReq.UpdateString("LineItems[0].Description","Consulting services as agreed (20% off standard rate)")
success = jsonReq.UpdateString("LineItems[0].Quantity","10")
success = jsonReq.UpdateString("LineItems[0].UnitAmount","100.00")
success = jsonReq.UpdateString("LineItems[0].AccountCode","200")
success = jsonReq.UpdateString("LineItems[0].DiscountRate","20")

set sbReq = CreateObject("Chilkat_9_5_0.StringBuilder")
success = jsonReq.EmitSb(sbReq)

success = rest.AddHeader("Content-Type","application/json")

set sbResponse = CreateObject("Chilkat_9_5_0.StringBuilder")
success = rest.FullRequestSb("POST","/api.xro/2.0/Invoices",sbReq,sbResponse)
If (success <> 1) Then
    outFile.WriteLine(rest.LastErrorText)
    WScript.Quit
End If

If (rest.ResponseStatusCode <> 200) Then
    outFile.WriteLine("Received error response code: " & rest.ResponseStatusCode)
    outFile.WriteLine("Response body:")
    outFile.WriteLine(sbResponse.GetAsString())
    WScript.Quit
End If

outFile.WriteLine("Example Completed.")

outFile.Close

Sample JSON Request Body

{
  "Type": "ACCREC",
  "Contact": { 
    "ContactID": "eaa28f49-6028-4b6e-bb12-d8f6278073fc" 
  },
  "Date": "\/Date(1518685950940+0000)\/",
  "DueDate": "\/Date(1518685950940+0000)\/",
  "DateString": "2009-05-27T00:00:00",
  "DueDateString": "2009-06-06T00:00:00",
  "LineAmountTypes": "Exclusive",
  "LineItems": [
    {
      "Description": "Consulting services as agreed (20% off standard rate)",
      "Quantity": "10",
      "UnitAmount": "100.00",
      "AccountCode": "200",
      "DiscountRate": "20"
    }
  ]
}