AtlatestRepositorysigil-lemonsqueezy
sigil-lemonsqueezy / treeREADME.md
1
# sigil-lemonsqueezy3
Lemon Squeezy API client library for Sigil. Provides functions for managing4
products, checkouts, orders, subscriptions, webhooks, and license keys via5
the [Lemon Squeezy API](https://docs.lemonsqueezy.com/api).7
## Features9
- **JSON:API parsing** — Helpers for extracting data, attributes,10
relationships, and included resources from JSON:API responses11
- **Products & Variants** — List and retrieve products and their variants12
- **Checkouts** — Create checkout URLs with custom data, product options,13
and pricing overrides14
- **Orders & Order Items** — List and retrieve orders and line items15
- **Subscriptions** — List, retrieve, update, and cancel subscriptions16
- **Webhooks** — CRUD operations plus HMAC-SHA256 signature verification17
- **License Keys** — Manage keys via the main API; validate, activate, and18
deactivate via the unauthenticated License API19
- **Pagination** — Built-in support for page-based pagination and filtering21
## Modules23
| Module | Description |24
|--------|-------------|25
| `(lemonsqueezy)` | Core client, auth, JSON:API helpers, pagination |26
| `(lemonsqueezy product)` | Products and variants |27
| `(lemonsqueezy checkout)` | Checkout creation and retrieval |28
| `(lemonsqueezy order)` | Orders, order items, subscriptions |29
| `(lemonsqueezy webhook)` | Webhook CRUD and signature verification |30
| `(lemonsqueezy license)` | License key management and validation |32
## Quick Start34
```scheme35
(import (lemonsqueezy)36
(lemonsqueezy product)37
(lemonsqueezy order)38
(lemonsqueezy checkout))40
;; Create a client with your API key41
(define client (ls-client api-key: "your-api-key"))43
;; List products44
(define products (ls-products client))45
(for-each (lambda (p)46
(display (ls-product-name p))47
(display " - ")48
(display (ls-product-price-formatted p))49
(newline))50
products)52
;; Get a single product53
(define product (ls-product-get client "123"))55
;; Create a checkout with custom data56
(define checkout57
(ls-create-checkout client "1" "10"58
#{ checkout-data: #{ email: "[email protected]"59
custom: #{ user_id: "42" } } }))60
(display (ls-checkout-url checkout))62
;; List orders with filtering63
(define orders64
(ls-orders client #{ filter: #{ store_id: "1" } }))65
```67
## Webhooks69
Verify incoming webhook signatures and parse event payloads:71
```scheme72
(import (lemonsqueezy)73
(lemonsqueezy webhook))75
;; Verify the X-Signature header76
(if (ls-verify-webhook signing-secret raw-request-body x-signature-header)77
(let ((event (parse-webhook-event raw-request-body)))78
(let ((event-name (ls-webhook-event-name event))79
(custom-data (ls-webhook-event-custom-data event)))80
;; Dispatch by event name81
(cond82
((string=? event-name "order_created")83
;; Grant access using custom_data.user_id84
(grant-access (dict-ref custom-data user_id:)))85
((string=? event-name "subscription_expired")86
;; Revoke access87
(revoke-access (dict-ref custom-data user_id:))))))88
(error "Invalid webhook signature"))89
```91
## License Keys93
The License API endpoints (validate, activate, deactivate) do not require94
an API key and are designed for use in client applications:96
```scheme97
(import (lemonsqueezy license))99
;; Validate a license key (no auth needed)100
(define result (ls-validate-license "38b1460a-5104-4067-a91d-77b872934d51"))101
(if (ls-license-validation-valid result)102
(display "License is valid!")103
(display "License is invalid"))105
;; Activate a license key106
(define activation107
(ls-activate-license "38b1460a-5104-4067-a91d-77b872934d51" "mysite.com"))109
;; Deactivate110
(ls-deactivate-license "38b1460a-5104-4067-a91d-77b872934d51" "instance-uuid")111
```113
## JSON:API Helpers115
Lemon Squeezy uses JSON:API format. The core module provides helpers:117
```scheme118
;; Extract data from a response119
(jsonapi-data response) ; single resource120
(jsonapi-data-list response) ; list of resources121
(jsonapi-attr resource name:) ; get attribute122
(jsonapi-attrs resource) ; full attributes dict124
;; Relationships125
(jsonapi-relationship-id resource store:) ; single relationship ID126
(jsonapi-relationship-ids resource variants:) ; has-many relationship IDs128
;; Included resources129
(jsonapi-included response) ; all included130
(jsonapi-find-included response "variants" "10") ; find by type+id132
;; Pagination133
(jsonapi-pagination-meta response) ; => #{ current-page: 1 last-page: 3 ... }134
(ls-paginate (lambda (page) ...)) ; auto-paginate all pages135
```137
## Pagination and Filtering139
All list endpoints accept an options dict:141
```scheme142
;; Paginate143
(ls-products client #{ page: 2 per-page: 50 })145
;; Filter146
(ls-subscriptions client #{ filter: #{ status: "active" product_id: "1" } })148
;; Include related resources149
(ls-products client #{ include: "variants,store" })151
;; Auto-paginate all pages152
(ls-paginate153
(lambda (page)154
(ls-get/json client155
(string-append156
(ls-api-url client "v1" "products")157
(build-query-string (ls-list-params #{ page: page per-page: 100 }))))))158
```160
## Building162
```bash163
sigil deps install164
sigil build165
sigil test166
```168
## Dependencies170
- sigil-stdlib (core, dict, string, struct, crypto)171
- sigil-http (HTTP client)172
- sigil-json (JSON parsing)174
## API Coverage176
| Resource | Operations |177
|----------|-----------|178
| Products | list, get |179
| Variants | list, get |180
| Checkouts | create, get, list |181
| Orders | list, get |182
| Order Items | list, get |183
| Subscriptions | list, get, update, cancel |184
| Webhooks | create, get, list, update, delete, verify |185
| License Keys | list, get, update (main API) |186
| License API | validate, activate, deactivate (no auth) |188
## License190
BSD-3-Clause