Code samples
Generate SDK code samples from CLI commands without sending an API request.
chargebee customer create -d first_name=Ada -d email=ada@example.com --code-sample pythonThe command prints a Python code sample from Chargebee's code-sample generator. You can generate samples without connecting a site. Samples use a placeholder API key and, when no site is configured, a placeholder site name.
Supported code-sample languages
Pass one of these values to --code-sample. Each generates code for the current version of that language's SDK; the older-version aliases generate code for the previous SDK version.
| Language | Value | Also accepted | Older SDK version |
|---|---|---|---|
| cURL | curl | None | None |
| Python | python | python-v3 | python-v2 |
| Node.js | nodejs | node javascript node-v3 | node-v2 |
| Go | go | go-v4 | go-v3 |
| Java | java | java-v4 | java-v3 |
| PHP | php | php-v4 | php-v3 |
| Ruby | ruby | None | None |
| .NET | dotnet | csharp | None |
chargebee customer create --code-sample list # print supported languages-s is the short form of --code-sample:
chargebee invoice list -d limit=5 -s nodejsJSON input is also supported:
echo '{"email":"ada@example.com"}' | chargebee customer create - --code-sample goCLI parameter checks for code samples
The CLI validates parameter names and required parameters before generating code. For example, an unknown field is rejected:
chargebee customer create -d emali=ada@example.com --code-sample curlOutput:
Validation failed for create_a_customer: emali: Unknown field "emali"Site-specific cf_ custom fields are allowed. The CLI does not validate value types or allowed values. Check those in the API reference. These checks apply to code samples only, not to live API calls.
Product Catalog checks for code samples
Samples follow the Product Catalog detected for the connected site. On a Product Catalog 2.0 site, use subscription create-with-items; generating a sample for the 1.0 operation subscription create is blocked.
When the catalog is unknown, the CLI skips this check.
Code-sample generation without API requests
Code-sample generation does not make an API request, so it does not use the live-site confirmation. Before running a generated sample, check the site and API key it uses and make sure they point to the intended site. A sample using a live API key can change production data.
Generate CLI code samples
# The exact list request you are running, as Node.js
chargebee customer list -d 'email[is]=ada@example.com' --code-sample nodejs
# A cURL request for a webhook debugging session
chargebee event retrieve ev_123 --code-sample curl
# Java for creating a subscription on a Product Catalog 2.0 site
chargebee subscription create-with-items cust_123 \
-d 'subscription_items=[{"item_price_id":"standard-USD-monthly"}]' \
--code-sample javaRelated code-sample guides
- Client libraries: install and configure the SDK the sample uses
- Commands: command shape and parameter syntax