Create & update Pivot Tables
The Google Sheets API documentation contains:
A guide describing how and why to use the Google Sheets API to create pivot tables in your spreadsheets:
Examples illustrating how you can achieve some common pivot table operations with the API:
The Apps Script documentation also contains a code sample to create a pivot table.
Here are more code samples, showing how to create a simple Pivot Table or update an existing one with Apps Script and the Advanced Sheets Service.
Create a simple Pivot Table
This example is based on the following spreadsheet and creates a pivot table with minimal parameters (the source range, a grouping by row and values summarized by COUNTA) to display the number of entries / people by country.
Edit / Update an existing Pivot Table
The documentation indicates that "the entire pivotTable field must be supplied to make edits. Essentially, editing a pivot table requires replacing it with a new one."
In this example, we use the API to get settings of an existing Pivot Table, update one of those settings (the source range) and push this change via a new API call.