Sample code for 30+ languages & platforms
Unicode C

Xero Create Account

See more Xero Examples

Create new accounts in a Xero company.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkJsonObjectW jsonToken;
    HCkJsonObjectW jsonRequestBody;
    const wchar_t *url;
    HCkHttpResponseW resp;
    HCkJsonObjectW jsonResponse;
    const wchar_t *AccountID;
    const wchar_t *Code;
    const wchar_t *Name;
    const wchar_t *Type;
    const wchar_t *TaxType;
    const wchar_t *Class;
    BOOL EnablePaymentsToAccount;
    BOOL ShowInExpenseClaims;
    const wchar_t *ReportingCode;
    const wchar_t *ReportingCodeName;
    const wchar_t *UpdatedDateUTC;
    BOOL AddToWatchlist;
    const wchar_t *Id;
    const wchar_t *Status;
    const wchar_t *ProviderName;
    const wchar_t *DateTimeUTC;
    int i;
    int count_i;

    success = FALSE;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    http = CkHttpW_Create();

    jsonToken = CkJsonObjectW_Create();
    success = CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/xero-access-token.json");
    if (success == FALSE) {
        wprintf(L"%s\n",CkJsonObjectW_lastErrorText(jsonToken));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(jsonToken);
        return;
    }

    CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(jsonToken,L"access_token"));

    //  Replace the value here with an actual tenant ID obtained from this example:
    //  Get Xero Tenant IDs
    CkHttpW_SetRequestHeader(http,L"Xero-tenant-id",L"83299b9e-5747-4a14-a18a-a6c94f824eb7");

    CkHttpW_putAccept(http,L"application/json");

    //  The following JSON is sent in the request body:

    //  {
    //    "Code": "201",
    //    "Name": "Sales - clearance lines",
    //    "Type": "SALES"
    //  }

    //  Use this online tool to generate the code from sample JSON: 
    //  Generate Code to Create JSON

    jsonRequestBody = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(jsonRequestBody,L"Code",L"201");
    CkJsonObjectW_UpdateString(jsonRequestBody,L"Name",L"Sales - clearance lines");
    CkJsonObjectW_UpdateString(jsonRequestBody,L"Type",L"SALES");

    url = L"https://api.xero.com/api.xro/2.0/Accounts";

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpJson(http,L"PUT",url,jsonRequestBody,L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(jsonToken);
        CkJsonObjectW_Dispose(jsonRequestBody);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    wprintf(L"Response Status Code: %d\n",CkHttpResponseW_getStatusCode(resp));

    jsonResponse = CkJsonObjectW_Create();
    CkJsonObjectW_Load(jsonResponse,CkHttpResponseW_bodyStr(resp));
    CkJsonObjectW_putEmitCompact(jsonResponse,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse));

    if (CkHttpResponseW_getStatusCode(resp) >= 300) {
        wprintf(L"Failed.\n");
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(jsonToken);
        CkJsonObjectW_Dispose(jsonRequestBody);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(jsonResponse);
        return;
    }

    //  Sample output...
    //  (See the parsing code below..)
    //  
    //  Use the this online tool to generate parsing code from sample JSON: 
    //  Generate Parsing Code from JSON

    //  {
    //    "Id": "705036aa-771d-4c0a-9d66-28904022858c",
    //    "Status": "OK",
    //    "ProviderName": "Chilkat2222",
    //    "DateTimeUTC": "\/Date(1587161712234)\/",
    //    "Accounts": [
    //      {
    //        "AccountID": "54ddab14-4a8d-45cf-86be-076c99a0cea0",
    //        "Code": "201",
    //        "Name": "Sales - clearance lines",
    //        "Status": "ACTIVE",
    //        "Type": "SALES",
    //        "TaxType": "OUTPUT",
    //        "Class": "REVENUE",
    //        "EnablePaymentsToAccount": false,
    //        "ShowInExpenseClaims": false,
    //        "ReportingCode": "REV",
    //        "ReportingCodeName": "Revenue",
    //        "UpdatedDateUTC": "\/Date(1587161712283+0000)\/",
    //        "AddToWatchlist": false
    //      }
    //    ]
    //  }
    //  

    //  Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    //  See this example explaining how this memory should be used: const char * functions.

    Id = CkJsonObjectW_stringOf(jsonResponse,L"Id");
    Status = CkJsonObjectW_stringOf(jsonResponse,L"Status");
    ProviderName = CkJsonObjectW_stringOf(jsonResponse,L"ProviderName");
    DateTimeUTC = CkJsonObjectW_stringOf(jsonResponse,L"DateTimeUTC");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"Accounts");
    while (i < count_i) {
        CkJsonObjectW_putI(jsonResponse,i);
        AccountID = CkJsonObjectW_stringOf(jsonResponse,L"Accounts[i].AccountID");
        Code = CkJsonObjectW_stringOf(jsonResponse,L"Accounts[i].Code");
        Name = CkJsonObjectW_stringOf(jsonResponse,L"Accounts[i].Name");
        Status = CkJsonObjectW_stringOf(jsonResponse,L"Accounts[i].Status");
        Type = CkJsonObjectW_stringOf(jsonResponse,L"Accounts[i].Type");
        TaxType = CkJsonObjectW_stringOf(jsonResponse,L"Accounts[i].TaxType");
        Class = CkJsonObjectW_stringOf(jsonResponse,L"Accounts[i].Class");
        EnablePaymentsToAccount = CkJsonObjectW_BoolOf(jsonResponse,L"Accounts[i].EnablePaymentsToAccount");
        ShowInExpenseClaims = CkJsonObjectW_BoolOf(jsonResponse,L"Accounts[i].ShowInExpenseClaims");
        ReportingCode = CkJsonObjectW_stringOf(jsonResponse,L"Accounts[i].ReportingCode");
        ReportingCodeName = CkJsonObjectW_stringOf(jsonResponse,L"Accounts[i].ReportingCodeName");
        UpdatedDateUTC = CkJsonObjectW_stringOf(jsonResponse,L"Accounts[i].UpdatedDateUTC");
        AddToWatchlist = CkJsonObjectW_BoolOf(jsonResponse,L"Accounts[i].AddToWatchlist");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(jsonToken);
    CkJsonObjectW_Dispose(jsonRequestBody);
    CkHttpResponseW_Dispose(resp);
    CkJsonObjectW_Dispose(jsonResponse);

    }