Swift Requires Chilkat v11.7.0+
Swift
Stripe: Export a period of account activity
See more Stripe Examples
Export account activity for a UTC date range to CSV, including transaction references, fees, net amounts, and currencies.
Stripe documentation and cURL example
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Requires an unlocked Chilkat bundle. See Global Unlock Sample.
// Supply a restricted sandbox key from merchant-controlled configuration.
var apiKey: String? = "YOUR_STRIPE_TEST_KEY"
let curl = CkoHttpCurl()!
let cmd = CkoStringBuilder()!
let json = CkoJsonObject()!
var replaced: Int
// Example UTC interval: 1 October 2026 inclusive through 1 November exclusive.
var fromTime: String? = "1790812800"
var untilTime: String? = "1793491200"
let ledger = CkoCsv()!
ledger.hasColumnNames = true
var outputRow: Int = 0
ledger.setColumnName(index: 0, columnName: "id")
ledger.setColumnName(index: 1, columnName: "created")
ledger.setColumnName(index: 2, columnName: "source")
ledger.setColumnName(index: 3, columnName: "reporting_category")
ledger.setColumnName(index: 4, columnName: "amount")
ledger.setColumnName(index: 5, columnName: "fee")
ledger.setColumnName(index: 6, columnName: "net")
ledger.setColumnName(index: 7, columnName: "currency")
let pageSuffix = CkoStringBuilder()!
var more: Bool = true
var cursor: String? = ""
while (more == true) {
// curl -X GET "https://api.stripe.com/v1/balance_transactions?created%5Bgte%5D={fromTime}&created%5Blt%5D={untilTime}&limit=100" \
// -u "$STRIPE_API_KEY:" \
// -H "Stripe-Version: 2026-10-28.endive"
cmd.setString(value: "curl -X GET \"https://api.stripe.com/v1/balance_transactions?created%5Bgte%5D=@FROM@&created%5Blt%5D=@UNTIL@&limit=100@CURSOR@\"")
cmd.append(value: " -u \"@KEY@:\"")
cmd.append(value: " -H \"Stripe-Version: 2026-10-28.endive\"")
replaced = cmd.replace(value: "@CURSOR@", replacement: pageSuffix.getAsString()).intValue
replaced = cmd.replace(value: "@FROM@", replacement: fromTime).intValue
replaced = cmd.replace(value: "@UNTIL@", replacement: untilTime).intValue
replaced = cmd.replace(value: "@KEY@", replacement: apiKey).intValue
success = curl.doYourThing(targetCurl: cmd.getAsString())
if success != true {
print("No HTTP response. Chilkat failure code: \(curl.failReason.intValue)")
return
}
success = curl.getResponseJson(json: json)
if (curl.statusCode.intValue != 200) || (success != true) {
print("Stripe HTTP \(curl.statusCode.intValue) Request-Id: \(curl.getResponseHeaderField(name: "Request-Id")!)")
if success == true {
print("\(json.string(of: "error.message")!)")
}
return
}
var i: Int = 0
var count: Int = json.size(ofArray: "data").intValue
while (i < count) {
json.i = i
ledger.setCell(row: outputRow, col: 0, content: json.string(of: "data[i].id"))
ledger.setCell(row: outputRow, col: 1, content: json.string(of: "data[i].created"))
ledger.setCell(row: outputRow, col: 2, content: json.string(of: "data[i].source"))
ledger.setCell(row: outputRow, col: 3, content: json.string(of: "data[i].reporting_category"))
ledger.setCell(row: outputRow, col: 4, content: json.string(of: "data[i].amount"))
ledger.setCell(row: outputRow, col: 5, content: json.string(of: "data[i].fee"))
ledger.setCell(row: outputRow, col: 6, content: json.string(of: "data[i].net"))
ledger.setCell(row: outputRow, col: 7, content: json.string(of: "data[i].currency"))
outputRow = outputRow + 1
i = i + 1
}
more = json.bool(of: "has_more")
if more == true {
var next: String? = json.string(of: "data[i].id")
if (count < 1) || (next == "") || (next == cursor) {
print("Invalid pagination cursor.")
return
}
cursor = next
pageSuffix.setString(value: "&starting_after=")
pageSuffix.append(value: cursor)
}
}
success = ledger.saveFile(path: "stripe-balance-transactions.csv")
if success == false {
print("\(ledger.lastErrorText!)")
return
}
print("Exported rows: \(outputRow)")
}