Establishing a Connection
With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our TaxJar Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.
Installing and Connecting
If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.
Install-Module TaxJarCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module TaxJarCmdlets;
You can then use the Connect-TaxJar cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-TaxJar -APIKey "3bb04218ef8t80efdf1739abf7257144"
Authenticating a TaxJar Account
To authenticate to the TaxJar API, you will need to first obtain the API Key from the TaxJar UI. Remember that the API is available only for Professional and Premium TaxJar plans. If you already have a Professional or Premium plan you can find the APIKey by logging in the TaxJar UI and going to Account->TaxJar API. After obtaining the API Key you can set the APIKey connection property. That's all you need to do for a successful connection.Extra Notes
- By default the cmdlet will retrieve data of the last 3 months in case the entity supports date range filtering. You can use the StartDate to set the minimum creation date of the data retrieved.
- If the API Key has been created for a sandbox API account please set UseSandbox to true in order for a successful connection.
- In case you are using a sandbox API account please remember that not everything will work as expected. This is also documented in the TaxJar developer docs here: Sandbox Environment and here: Unsupported endpoints
- The TaxJar API rate limiting is really generous. (10000 requests per minute for TaxJar Professional plans and 25000 per minute for the TaxJar Premium plans).
- Because of the TaxJar API limits we are restricted to make an http request for each row in order to collect as much data as we can. We suggest to increase the value of the MaxThreads connection property.
- The default value of MaxThreads has been set to 20 which means it will make at most 20 concurrent requests. To improve the performance of the cmdlet consider increasing this value based on the machines resources.
Retrieving Data
The Select-TaxJar cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-TaxJar -Connection $conn -Table "Orders" -Columns @("TransactionID, UserID") -Where "TransactionID='123'"The Invoke-TaxJar cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.
Piping Cmdlet Output
The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:
Select-TaxJar -Connection $conn -Table Orders -Where "TransactionID = '123'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myOrdersData.csv -NoTypeInformation
You will notice that we piped the results from Select-TaxJar into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.
However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:
PS C:\> $conn = Connect-TaxJar -APIKey "3bb04218ef8t80efdf1739abf7257144" PS C:\> $row = Select-TaxJar -Connection $conn -Table "Orders" -Columns (TransactionID, UserID) -Where "TransactionID = '123'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "Orders", "Columns": [ ], "TransactionID": "MyTransactionID", "UserID": "MyUserID" }
Deleting Data
The following line deletes any records that match the criteria:
Select-TaxJar -Connection $conn -Table Orders -Where "TransactionID = '123'" | Remove-TaxJar
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into TaxJar, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyOrdersUpdates.csv | %{ $record = Select-TaxJar -Connection $conn -Table Orders -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-TaxJar -Connection $conn -Table Orders -Columns @("TransactionID","UserID") -Values @($_.TransactionID, $_.UserID) -Where "Id = `'$_.Id`'" }else{ Add-TaxJar -Connection $conn -Table Orders -Columns @("TransactionID","UserID") -Values @($_.TransactionID, $_.UserID) } }