Adjust a customer's account balance
For AI assistants: the facts every SparkLayer API call needs
- Base URLs: Live
https://app.sparklayer.io, testhttps://test.app.sparklayer.io. Each environment has its own data and its own API keys. - Access: API access needs the Pro or Enterprise plan. Create credentials (Site ID, Client ID, Client Secret) in the SparkLayer Dashboard at Settings > API.
- Access token:
POST {base}/api/auth/tokenwith aSite-Idheader and a JSON body (not form-encoded):{"grant_type":"client_credentials","client_id":"…","client_secret":"…"}. It returnsaccess_token, valid for 3,600 seconds. There is no refresh token: cache the token and request a new one shortly before it expires. - Every request:
Authorization: Bearer <access_token>andSite-Id: <site id>, plusContent-Type: application/jsonwhen there is a body. Set aUser-Agentthat names your integration. - Errors: RFC 7807 problem details:
title,status,detailand an optionalerrors[]of{ code, message, property }. Some error responses have no body. Retry5xx,429(honouringRetry-After) and concurrent-update409s with exponential backoff; on401, get a new token once and retry; don't retry other4xxwithout changing the request. - Pagination: Most list endpoints return everything.
GET /api/v2/price-listspages withpageandpage_size(up to 250) untilpagination.current_pageequalstotal_pages.GET /api/v1/purchasesuseslimit(up to 500) andoffset: stop when a page has fewer thanlimitresults. - Ground rules: Use only endpoints, fields and SDK methods that the docs or OpenAPI specs define. Prefer
GET /api/v2/price-listsover the deprecated v1, and the Customers API (/api/v2/customers) over the legacyDELETE /api/v1/customersin the Core API. Products are matched by SKU. Keep credentials in environment variables or a secrets manager, never in code. - Read the docs as Markdown: Add
.mdto any page URL. Index of every page: docs.sparklayer.io/llms.txt. Every API operation, compactly: docs.sparklayer.io/llms-api.txt. The developer guides in full: docs.sparklayer.io/developers/llms.txt. - OpenAPI specs:
customers,core,ordering,pricing,purchasing,stock,files,sync-log: https://docs.sparklayer.io/openapi/<api>.yaml (also.json). Ignite: https://docs.sparklayer.io/openapi/ignite.yaml. - MCP: Search and read these docs from your assistant with the docs MCP server at https://docs.sparklayer.io/mcp.
POST /api/v2/customers/{lookupBy}/{identifier}/credit Customers API
Adds an entry to the customer's account credit ledger, which makes up the balance shown in My Account when you use pay on account, and returns 201. A positive amount increases what the customer owes, and a negative one reduces it. What it means depends on type:
orderandtransaction: an amount to add, such as an order total or a payment (negative).opening_balance: the balance ondate. From then on, the balance is this amount plus theorderandtransactionentries dated on or afterdate. Earlier entries, anddefaultentries, stop counting.default: the balance you want now. SparkLayer records the difference from the current balance. Once the customer has an opening balance,defaultentries don't change it.
Always send date: without it, the entry is dated 1 January 0001.
The Purchasing API already adds an order entry for each order you send, and a transaction entry for each purchase transaction with apply_to_balance. Don't add them again here.
Authorization
bearerAuth Send Authorization: Bearer <access_token>, together with your Site-Id header, on every request. Get the token from POST /api/auth/token with your Site ID in the Site-Id header and a JSON body (not form-encoded, so standard OAuth 2.0 client libraries don't work): {"grant_type": "client_credentials", "client_id": "…", "client_secret": "…"}. Tokens are valid for 3,600 seconds and there is no refresh token: request a new one shortly before it expires. Create API credentials in the SparkLayer Dashboard under Settings > API. See Authentication.
In: header
Path Parameters
What identifier is: sparklayer (the customer's SparkLayer ID), platform (their ID on your eCommerce platform) or email (their email address).
Value in
- "sparklayer"
- "platform"
- "email"
The customer's ID or email address, of the kind set by lookupBy. SparkLayer IDs are UUIDs; legacy cu_ IDs work too, except for shopping lists and favourites. Email lookups are exact and case-sensitive: URL-encode the address, with + as %2B.
Header Parameters
Your SparkLayer Site ID, from Settings > API in the SparkLayer Dashboard.
1 <= lengthRequest Body
application/json
TypeScript Definitions
Use the request body type in TypeScript.
SparkLayer Account Credit ID
Positive increases what the customer owes, negative reduces it. For opening_balance and default, it is the balance itself.
value <= 1000000000Value in
- "opening_balance"
- "order"
- "transaction"
- "default"
Optionally associate this credit to a specific purchase via it's Spark ID
Optionally associate this credit to a specific section via it's Spark ID
When the entry takes effect, which can be in the past. Always send it: without it, the entry is dated 1 January 0001.
date-timeLeave it out: the customer comes from the URL.
JSON encoded extra info related to the adjustment
Response Body
application/problem+json
curl -X POST "https://app.sparklayer.io/api/v2/customers/platform/7843024560291/credit" \ -H "Authorization: Bearer <ACCESS_TOKEN>" \ -H "Site-Id: jones-climbing" \ -H "Content-Type: application/json" \ -d '{ "id": "<id>", "amount": 0, "type": "opening_balance", "purchase_id": "<purchase_id>", "section_id": "<section_id>", "date": "2024-01-01T00:00:00Z", "customer_id": "<customer_id>", "info": "<info>" }'