SellMyCode Developers
Licences

Licence activation

For software that phones home — PHP scripts, desktop or mobile apps. Your installation sends the purchase code and its domain; we confirm the licence and count how many sites it runs on, so you do not have to host a licensing server yourself.

ℹ️

Note: These endpoints need NO API token — the purchase code is the credential, so the code you ship to buyers can call them directly.

⚠️

Warning: Every answer is signed with Ed25519. Verify the signature and the nonce before trusting it. A module that only reads status can be fooled by a fake server or a hosts-file redirect that answers "success". See Signatures & fields.

ℹ️

Note: The right to USE the software never expires. When update support ends, verify still returns valid — only the updates block reports that renewal is due. An expired update period must never break a live site.

⚠️

Important: item_id is required on activate, verify and deactivate. Every call is scoped to the calling item, so a buyer who types the code of another product into yours gets invalid_code — nothing is activated on that other licence and no slot is taken. Calls without item_id are refused with invalid_request.

#Endpoints

POST/api/license/activate
POST/api/license/verify
POST/api/license/deactivate
GET/api/license/public-key
Parameter Type Description
purchase_code required string the code the buyer received
item_id required integer the ID of YOUR item — the number at the end of its SellMyCode URL (also shown in Workspace → My Items). Hard-code it in your product. A purchase code of any other item is answered exactly like an unknown code (invalid_code) and never activates or holds a slot.
domain required string the site running the software; scheme, www and path are stripped automatically
nonce required string 16–128 random characters (A-Z a-z 0-9 _ -), new for EVERY call. It is echoed inside the signed answer, so an old answer cannot be replayed.
activation_token string the token you received when this site was activated — required by deactivate; optional on verify and activate, where it adds an entitlement token to the answer

#What your software must do

  1. Send a fresh random nonce with every call.
  2. Verify the signature over the payload string with the public key. If it fails, ignore the answer completely.
  3. Decode the payload and check that its nonce equals the one you sent, that item.id is YOUR item and that domain is this site. One key signs answers for every product on SellMyCode, so a relay could pass on a genuine answer about another item or another site with your nonce in it.
  4. On activate, store activation_token — it is shown once, and deactivation, updates and entitlement tokens all need it.
  5. On activation_limit_reached, show the hint and a button that opens transfer_url (the item's support page), where the buyer asks you to free a slot.
  6. On license_blocked or site_blocked, switch the licensed features off and show the hint with transfer_url — these are authoritative signed answers, like invalid_code.
  7. Keep the last verified answer. When we cannot be reached, answer 5xx, or rate limit you, keep running on it until its grace_until — never treat an outage as "licence invalid". A signed error such as invalid_code or not_activated is authoritative and should replace it.
// PHP 7.2+ (sodium is built in)
function sellmycode_license(string $action, array $fields): ?array
{
    // kid => raw 32-byte public key (Base64): the active key and the standby "next" key
    $keys = ['k1' => '5Uk3p1+J0PXzJkwS4wVFvTN9GcHpJzX5ClRI8zlI0yQ=', 'k2' => 'erOiojOdS5F/IfhPMO9gvjAvuEPVapIadTDaWU0z83o='];
    $fields['nonce'] = bin2hex(random_bytes(16));

    $ch = curl_init('https://sellmycode.net/api/license/' . $action);
    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 15,
        CURLOPT_POSTFIELDS => http_build_query($fields),
    ]);
    $res = json_decode((string) curl_exec($ch), true);

    // unreachable, 5xx, unsigned or forged: return null and keep the cached state
    if (!isset($res['payload'], $res['signature'], $keys[$res['kid'] ?? ''])
        || !sodium_crypto_sign_verify_detached(base64_decode($res['signature']), $res['payload'], base64_decode($keys[$res['kid']]))) {
        return null;
    }
    $data = json_decode($res['payload'], true);

    // must answer THIS request, not be an old answer replayed
    if (($data['nonce'] ?? '') !== $fields['nonce']) {
        return null;
    }
    // and be about THIS item and THIS site: one key signs for every product on SellMyCode
    if (isset($data['item']['id']) && (int) $data['item']['id'] !== (int) $fields['item_id']) {
        return null;
    }
    if (isset($data['domain']) && $data['domain'] !== sellmycode_domain($fields['domain'])) {
        return null;
    }
    return $data;
}

// the domain as SellMyCode stores it: lower case, no scheme, path or www.
function sellmycode_domain(string $value): string
{
    $host = parse_url(strpos($value, '://') === false ? 'http://' . $value : $value, PHP_URL_HOST);
    return preg_replace('/^www\./', '', strtolower(trim((string) $host, '.')));
}

const ITEM_ID = 12345; // your item's ID on SellMyCode

// first run: activate and keep the token
$r = sellmycode_license('activate', ['purchase_code' => $code, 'item_id' => ITEM_ID, 'domain' => $_SERVER['HTTP_HOST']]);
if ($r && $r['status'] === 'success' && $r['activation_token']) {
    save_setting('license_token', $r['activation_token']);
}

// daily: verify, cache the signed answer, run on it through an outage
$r = sellmycode_license('verify', ['purchase_code' => $code, 'item_id' => ITEM_ID, 'domain' => $_SERVER['HTTP_HOST']]);
if ($r && $r['code'] !== 'rate_limited') {
    save_setting('license_state', $r);
}
$state = get_setting('license_state');
$licensed = $state && $state['status'] === 'success' && strtotime($state['grace_until']) > time();

#Error codes

Error code HTTP Meaning
invalid_code 403 unknown, refunded or cancelled purchase code — or a code of another item than item_id (deliberately indistinguishable)
activation_limit_reached 403 every slot is taken (paused sites included) — show hint and transfer_url; the author can free one
license_blocked 403 the author blocked this whole licence over abuse — stop the licensed features and show the hint and transfer_url
site_blocked 403 the author blocked this domain for the licence — it cannot activate again and still holds its slot (slot_bound = true); other domains of the code are unaffected
not_activated 403 this domain is not licensed right now: never activated, paused (slot_bound = true — activate again) or revoked by the author (slot_released = true)
invalid_activation_token 403 deactivate: the token does not belong to this site
invalid_request 400 a parameter is missing or malformed — most often the nonce or item_id
invalid_domain 400 the domain could not be parsed
rate_limited 429 slow down and retry after retry_after seconds — not a licence problem, keep your cached state
signing_unavailable 503 temporary, and the only unsigned answer — keep your cached state

#Limits

30 calls per 10 minutes for each licence + domain, and 30 FAILED lookups per 10 minutes per IP. Valid calls do not count against the IP, so many sites sharing one hosting IP are not blocked.

ℹ️

Note: Set the number of allowed sites per licence when you publish the item (Activation limit, separately for Regular and Extended). Leave it empty for unlimited.

Was this page helpful?