Entitlements - Example Code Snippets

In the overview article, we explained the core concepts of entitlements and how they control access to features or content. In this article, we’ll look at practical examples.

Below you’ll find code snippets - examples on how to unlock content in PHP, Node.js, and JavaScript.

Example in PHP

This is example code in PHP. To find out more about entitlements, please refer to the overview article.

<?php
$cleengApiKey = "-7RhPJP4ZuaNdkA6dYPhv-d22Xv2Cs6_7c7QlhC0P85QsBXi"; //replace with your Publisher Token (API Key) from the dashboard
$cleengCustomerId = "123123123"; //
$cleengOfferId = "S123123123_US";

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://api.cleeng.com/3.1/entitlements/customer_id/".$cleengCustomerId."/offer_id/".$cleengOfferId,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 10,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => [
    "X-Publisher-Token: ".$cleengApiKey,
    "accept: application/json"
  ],
]);

$response = json_decode(curl_exec($curl),true);
curl_close($curl);

if ($response['accessGranted']) {
	echo "Load your premium assets and content";
} else {
	echo "<a href=\"/your-payment-page\">Buy now</a>";
		
}

echo "<pre>";
print_r($response); // debugging to see full response.
echo "</pre>";

?>

Example in Node.JS

This is example code in Node.JS. To find out more about entitlements, please refer to the overview article.

import fetch from "node-fetch"; // Node 18+: you can just use global fetch

const cleengApiKey = "-7RhPJP4ZuaNdkA6dYPhv-d22Xv2Cs6_7c7QlhC0P85QsBXi";
const cleengCustomerId = "123123123";
const cleengOfferId = "S123123123_US";

const url = `https://api.cleeng.com/3.1/entitlements/customer_id/${cleengCustomerId}/offer_id/${cleengOfferId}`;

async function checkEntitlement() {
  try {
    const response = await fetch(url, {
      method: "GET",
      headers: {
        "X-Publisher-Token": cleengApiKey,
        "accept": "application/json"
      },
      timeout: 10000 // 10 seconds
    });

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    const data = await response.json();

    if (data.accessGranted) {
      console.log("✅ Load your premium assets and content");
    } else {
      console.log("❌ Access denied — show paywall");
      console.log('<a href="/your-payment-page">Buy now</a>');
    }

    // Debugging: full API response
    console.log("Full response:", JSON.stringify(data, null, 2));
    
  } catch (err) {
    console.error("Request failed:", err.message);
  }
}

checkEntitlement();

Example in JavaScript

This is example code in JavaScript. To find out more about entitlements, please refer to the overview article.

const jwtToken = "your_JWT_here"; // replace with actual JWT
const offerId = "S123123123_US";

async function checkEntitlement() {
  try {
    const response = await fetch(
      `https://mediastoreapi.cleeng.com/entitlements/${offerId}`,
      {
        method: "GET",
        headers: {
          accept: "application/json",
          Authorization: `Bearer ${jwtToken}`,
        },
      }
    );

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    const data = await response.json();

    // entitlement logic
    if (data.accessGranted) {
      console.log("✅ Load your premium assets and content");
      // e.g., document.body.innerHTML = "<h1>Welcome premium user!</h1>";
    } else {
      console.log("❌ Access denied — show paywall");
      console.log('<a href="/your-payment-page">Buy now</a>');
      // e.g., document.body.innerHTML = '<a href="/your-payment-page">Buy now</a>';
    }

    // Debugging
    console.log("Full response:", JSON.stringify(data, null, 2));
  } catch (err) {
    console.error("Request failed:", err.message);
  }
}

checkEntitlement();