New in Chargebee: Explore Reveal and understand your payment performance end-to-end.

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 python

The 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.

LanguageValueAlso acceptedOlder SDK version
cURLcurlNoneNone
Pythonpythonpython-v3python-v2
Node.jsnodejsnode javascript node-v3node-v2
Gogogo-v4go-v3
Javajavajava-v4java-v3
PHPphpphp-v4php-v3
RubyrubyNoneNone
.NETdotnetcsharpNone
chargebee customer create --code-sample list     # print supported languages

-s is the short form of --code-sample:

chargebee invoice list -d limit=5 -s nodejs

JSON input is also supported:

echo '{"email":"ada@example.com"}' | chargebee customer create - --code-sample go

CLI 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 curl

Output:

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

You can generate samples for write operations on live sites because generation sends no API request. Before running a generated sample in your application, supply your own site and API key. The CLI's live-site write restriction applies to CLI requests, not to code you run separately.

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 java