PHP code example of codelet / sdk

1. Go to this page and download the library: Download codelet/sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codelet / sdk example snippets




use Codelet\Client;
use Codelet\Generated\Model\CancelSubscriptionRequest;
use Codelet\Generated\Model\IngestEventRequest;

$codelet = new Client(getenv('CODELET_API_KEY')); // cl_live_… or cl_test_…

// Ingest (auto idempotency_key when omitted)
$codelet->ingest(
    (new IngestEventRequest())
        ->setCustomerId('org_42')
        ->setMetric('api_call')
        ->setQuantity('1')
);

$me = $codelet->account->getMe();
$customers = $codelet->customers->listCustomers();
$codelet->subscriptions->cancelSubscription(
    'subs_…',
    (new CancelSubscriptionRequest())->setMode(CancelSubscriptionRequest::MODE_AT_PERIOD_END)
);

use Codelet\Generated\Model\CreateMetricRequest;

$metric = $codelet->metrics->createMetric(
    (new CreateMetricRequest())
        ->setName('api_request')
        ->setDisplayName('API Request')
        ->setAggregation('sum')
);

use Codelet\Generated\Model\CreateCreditPackRequest;

$pack = $codelet->creditPacks->createCreditPack(
    (new CreateCreditPackRequest())
        ->setMetric('api_request')
        ->setName('500 API requests')
        ->setPriceAmount('25.00')
        ->setCurrency('USD')
        ->setQuantityGranted('500')
);

use Codelet\Generated\Model\CreatePlanFixedFee;
use Codelet\Generated\Model\CreatePlanRequest;
use Codelet\Generated\Model\CreatePlanUsageFee;
use Codelet\Generated\Model\CreatePlanVersion;
use Codelet\Generated\Model\PlanBillingMode;
use Codelet\Generated\Model\PlanBillingPeriod;
use Codelet\Generated\Model\PlanFixedFeeType;
use Codelet\Generated\Model\PlanMetricBillingTiming;
use Codelet\Generated\Model\PlanMetricPricingModel;

$plan = $codelet->plans->createPlan(
    (new CreatePlanRequest())
        ->setName('Pro')
        ->setDescription('Usage-based Pro plan')
        ->setVersion(
            (new CreatePlanVersion())
                ->setCurrency('USD')
                ->setBillingPeriod(PlanBillingPeriod::MONTHLY)
                ->setBillingMode(PlanBillingMode::POSTPAID)
                ->setFixedFees([
                    (new CreatePlanFixedFee())
                        ->setFeeType(PlanFixedFeeType::PERIOD_CHARGE)
                        ->setName('Platform fee')
                        ->setAmount('29.00'),
                ])
                ->setUsageFees([
                    (new CreatePlanUsageFee())
                        ->setPricingModel(PlanMetricPricingModel::UNIT)
                        ->setBillingTiming(PlanMetricBillingTiming::IN_ARREARS)
                        ->setIncludedQuantity('1000')
                        ->setUnitPrice('0.002')
                        ->setMetric('api_request'),
                ])
        )
);

// Drafts are not checkout-ready until published
$codelet->plans->publishPlan($plan->getId());

use Codelet\Generated\Model\CreateCheckoutRequest;

$packCheckout = $codelet->checkout->createCheckout(
    (new CreateCheckoutRequest())
        ->setCustomerId('org_42')
        ->setPackId('pack_…')
        ->setSuccessUrl('https://yourapp.com/billing/success')
        ->setCancelUrl('https://yourapp.com/billing/cancel')
        ->setIdempotencyKey('purchase:order-123')
);
// Redirect the browser to $packCheckout->getCheckoutUrl()

$planCheckout = $codelet->checkout->createCheckout(
    (new CreateCheckoutRequest())
        ->setCustomerId('org_42')
        ->setPlanId('plan_…')
        ->setSuccessUrl('https://yourapp.com/welcome')
);
// Redirect the browser to $planCheckout->getCheckoutUrl()

use Codelet\CodeletException;
use Codelet\RateLimitException;

try {
    $codelet->ingest($request);
} catch (RateLimitException $e) {
    // 429
} catch (CodeletException $e) {
    // other API errors (status + body)
}