Skip to content

Accounts

Eze Rodriguez edited this page Mar 14, 2020 · 2 revisions

Get Accounts

accounts, err := xeroClient.Accounts.List(ctx, nil)
if err != nil {
    fmt.Println(fmt.Errorf("error getting accounts: %v", err))
}

To order account results you might use the same List method but passing a struct of type AccountListOptions as the second parameter specifying those fields that will be included as query parameters part of the request URL e.g:

opts := xero.AccountListOptions {
    Order: "",             // e.g "Name DESC"
}

orderedAccounts, err := xeroClient.Accounts.List(ctx, &opts)
...

This will always return a list of 0 or more Accounts

Get an Account

You can use the GetByID method to get a single Account. e.g:

accountID := "00000000-0000-0000-0000-000000000000"
account, err := xeroClient.Accounts.GetByID(ctx, accountID)
if err != nil {
    fmt.Println(fmt.Errorf("error: %v", err))
    return
}

fmt.Printf("%+v\n", account)

Create an Account

account := &xero.Account{
	Code: "12315",
	Name: "Some account",
	Type: xero.AccountTypeCurrentAsset,
}

a, err := xeroClient.Accounts.Create(ctx, account)
if err != nil {
    fmt.Println(fmt.Errorf("error: %v", err))
    return
}

Update an Account

// Existing Account
account := &xero.Account{
    AccountID: "00000000-0000-0000-0000-000000000000",
    Code: "12315",
    Name: "Some account",
    Type: xero.AccountTypeCurrentAsset,
}

// Update one field of the account
account.Name = "New name"

a, err := client.Accounts.Update(ctx, account)
if err != nil {
    return nil, err
}

fmt.Printf("New Account name: %s\n", a.Name)
Clone this wiki locally