PHP code example of ostdotcom / ost-sdk-php

1. Go to this page and download the library: Download ostdotcom/ost-sdk-php 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/ */

    

ostdotcom / ost-sdk-php example snippets


     

    // Declare connection parameters.

    /* Mandatory API parameters */

    $apiKey = '__abc'; // OBTAINED FROM DEVELOPER PAGE

    $apiSecret = '_xyz';  // OBTAINED FROM DEVELOPER PAGE

    /*
       The valid API endpoints are:
       1. Mainnet: "https://api.ost.com/mainnet/v2/"
       2. Testnet: "https://api.ost.com/testnet/v2/"
     */
    $apiBaseUrl = 'https://api.ost.com/testnet/v2/';

    /* Optional API parameters */

    // Connection timeout in seconds.
    $timeoutInSeconds = '60';

    $configParams = array();
    $configParams["timeout"] = $timeoutInSeconds;


    $params = array();
    $params['apiKey'] = $apiKey;
    $params['apiSecret'] = $apiSecret;
    $params['apiBaseUrl'] = $apiBaseUrl;
    $params["config"] = $configParams;

    $ostObj = new OSTSdk($params);
    

    $usersService = $ostObj->services->users;
    

    /* Mandatory API parameters */
    // No mandatory parameters.

    $createParams = array();
    $response = $usersService->create($createParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    /* Mandatory API parameters */

    // UserId of user for whom user details needs to be fetched.
    $userId = 'c2c__';

    $getParams = array();
    $getParams['user_id'] = $userId;

    $response = $usersService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    /* Mandatory API parameters */
    // No mandatory parameters.

    /* Optional API parameters */

    // Array of userIds for which data needs to be fetched.
    $userIdsArray = array("c2c__", "d2c__");

    // Pagination identifier from the previous API call response. Not needed for page one.
    $paginationIdentifier = 'e77y___';

    // Limit.
    $limit = 10;

    $getParams = array();
    $getParams['ids'] = $userIdsArray;
    $getParams['pagination_identifier'] = $paginationIdentifier;
    $getParams['limit'] = $limit;

    $response = $usersService->getList($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $devicesService = $ostObj->services->devices;
    

    /* Mandatory API parameters */

    // UserId of user for whom device needs to be created.
    $userId = 'c2c___';

    // Device address of user's device.
    $deviceAddress = '0x1Ea___';

    // Device API signer address.
    $apiSignerAddress = '0x5F8___';

    $createParams = array();
    $createParams['user_id'] = $userId;
    $createParams['address'] = $deviceAddress;
    $createParams['api_signer_address'] = $apiSignerAddress;

    $response = $devicesService->create($createParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    * Mandatory API parameters */

    // UserId of user for whom device details needs to be fetched.
    $userId = 'c2c___';

    // Device address of user's device.
    $deviceAddress = '0x1E___';

    $getParams = array();
    $getParams['user_id'] = $userId;
    $getParams['device_address'] = $deviceAddress;

    $response = $devicesService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    /* Mandatory API parameters */

    // UserId of user for whom device details needs to be fetched.
    $userId = 'c2c6___';

    /* Optional API parameters */

    // Pagination identifier from the previous API call response. Not needed for page one.
    $paginationIdentifier = 'eyJ___';

    // Array of device addresses of end user.
    $deviceAddressesArray = array("0x5906ae461eb6283cf15b0257d3206e74d83a6bd4","0xab248ef66ee49f80e75266595aa160c8c1abdd5a");

    // Limit.
    $limit = 10;

    $getParams = array();
    $getParams['user_id'] = $userId;
    $getParams['pagination_identifier'] = $paginationIdentifier;
    $getParams['addresses'] = $deviceAddressesArray;
    $getParams['limit'] = $limit;

    $response = $devicesService->getList($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $deviceManagersService = $ostObj->services->deviceManagers;
    

    // Mandatory API parameters

    // UserId of user for whom device manager details needs to be fetched.
    $userId = 'c2c___';

    $getParams = array();
    $getParams['user_id'] = $userId;

    $response = $deviceManagersService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $sessionsService = $ostObj->services->sessions;
    

    // Mandatory API parameters

    // UserId of user for whom session details needs to be fetched.
    $userId = 'c2c___';

    // Session address of user for which details needs to be fetched.
    $sessionAddress = '0x1Ea___';

    $getParams = array();
    $getParams['user_id'] = $userId;
    $getParams['session_address'] = $sessionAddress;

    $response = $sessionsService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    // Mandatory API parameters

    // UserId of user for whom session details needs to be fetched.
    $userId = 'c2c___';

    // Optional API parameters

    // Pagination identifier from the previous API call response. Not needed for page one.
    $paginationIdentifier = 'eyJs___';

    // Array of session addresses of end user.
    $sessionAddressesArray = array("0x59___","0xab___");

    // Limit.
    $limit = 10;

    $getParams = array();
    $getParams['user_id'] = $userId;
    $getParams['pagination_identifier'] = $paginationIdentifier;
    $getParams['addresses'] = $sessionAddressesArray;
    $getParams['limit'] = $limit;

    $response = $sessionsService->getList($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $rulesService = $ostObj->services->rules;
    

    /* Mandatory API parameters */
    // No mandatory parameters.

    $getParams = array();
    $response = $rulesService->getList($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $pricePointsService = $ostObj->services->pricePoints;
    

    // Mandatory API parameters

    // ChainId of your brand token economy.
    $chainId = 2000;

    $getParams = array();
    $getParams['chain_id'] = $chainId;

    $response = $pricePointsService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $transactionsService = $ostObj->services->transactions;
    

    // Mandatory API parameters

    // Token holder address of receiver.
    $transferTo = array("0xa31___", "0xa32___");

    // Amount of tokens to be transferred. You might need to increase precision of PHP depending on your use-case for transferAmount.
    // Example = ini_set('precision', 25);
    $transferAmount = array("1", "1");

    // Parameters onse, use the address having name "Direct Transfer".
    $directTransferRuleAddress = "0xe379___";

    // Optional API parameters

    // Name of the transaction. Eg. 'like', 'download', etc.
    // NOTE: Max length 25 characters (Allowed characters: [A-Za-z0-9_/s])
    $transactionName = 'like';

    // Transaction type. Possible values: 'company_to_user', 'user_to_user', 'user_to_company'.
    $transactionType = 'user_to_user';

    // Some extra information about transaction.
    // NOTE: Max length 125 characters (Allowed characters: [A-Za-z0-9_/s])
    $details = 'lorem_ipsum';

    // Additional transaction information. There is no dependency between any of the metaProperty keys.
    // However, if a key is present, its value cannot be null or undefined.
    $metaPropertyParams = array();
    $metaPropertyParams['name'] = $transactionName;
    $metaPropertyParams['type'] = $transactionType;
    $metaPropertyParams['details'] = $details;

    $executeParams = array();
    $executeParams['user_id'] = $companyUserId;
    $executeParams['to'] = $directTransferRuleAddress;
    $executeParams['raw_calldata'] = json_encode($rawCallData);
    $executeParams['meta_property'] = $metaPropertyParams;

    $response = $transactionsService->execute($executeParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    // Mandatory API parameters

    // Token holder address of receiver.
    $transferToAddress = '0xa31__';

    // Company token holder address.
    $companyTokenHolderAddress = '0xa963___';

    // Pay currency code. Supported currency codes are 'USD', 'EUR' and 'GBP'.
    $payCurrencyCode = 'USD';

    // In pay transaction, the transfer amounts are in pay currency (fiat currency like USD) which then are converted
    // into tokens. Use get price point detail API of Price Points module to get this value.
    $pricePoint = 0.020606673;

    // Price point needs to be passed in atto. Multiply the price point with 10^18. Also, this value should be a string. 
    // You might need to increase precision of PHP depending on your use-case. Example = ini_set('precision', 25);
    $intendedPricePointInAtto = (string)($pricePoint * 10**18);

    // Amount of Fiat to be transferred.
    $transferAmountInFiat = 0.1;

    // Transfer amount in wei needs to be passed in atto. Multiply the fiat transfer amount with 10^18. Also, this value should be a string. 
    // You might need to increase precision of PHP depending on your use-case. Example = ini_set('precision', 25);
    $fiatTransferAmountInAtto = (string)($transferAmountInFiat * 10**18);;

    // Parameters opertyParams['type'] = $transactionType;
    $metaPropertyParams['details'] = $details;

    $executeParams = array();
    $executeParams['user_id'] = $companyUserId;
    $executeParams['to'] = $payRuleAddress;
    $executeParams['raw_calldata'] = json_encode($rawCallData);
    $executeParams['meta_property'] = $metaPropertyParams;

    $response = $transactionsService->execute($executeParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    // Mandatory API parameters

    // UserId of end-user.
    $userId = 'ee8___';

    // Unique identifier of the transaction to be retrieved.
    $transactionId = 'f1d___';

    $getParams = array();
    $getParams['user_id'] = $userId;
    $getParams['transaction_id'] = $transactionId;
    $response = $transactionsService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    // Mandatory API parameters

    // UserId of end-user.
    $userId = 'ee89___';

    // Optional API parameters

    // Array of status values.
    $statusesArray = array('CREATED', 'SUBMITTED', 'SUCCESS', 'FAILED');
  
    // To get transactions between a specific time interval, add start timestamp and end timestamp. 
    $startTime = 1563260786;
    $endTime = 1563280786;

    // Name of the transaction. Eg. 'like', 'download', etc.
    // NOTE: Max length 25 characters (Allowed characters: [A-Za-z0-9_/s])
    $transactionName = 'like';

    // Transaction type. Possible values: 'company_to_user', 'user_to_user', 'user_to_company'.
    $transactionType = 'company_to_user';

    // NOTE: Max length 125 characters (Allowed characters: [A-Za-z0-9_/s])
    $details = 'lorem_ipsum';

    // Additional transaction information. There is no dependency between any of the metaProperty keys.
    // However, if a key is present, its value cannot be null or undefined.
    $metaPropertyArrayParams = array();
    $metaPropertyArrayParams['name'] = $transactionName;
    $metaPropertyArrayParams['type'] = $transactionType;
    $metaPropertyArrayParams['details'] = $details;
    $metaPropertiesArray = array($metaPropertyArrayParams);
    $metaPropertiesArrayJsonStr = json_encode($metaPropertiesArray);

    // Limit.
    $limit = 10;

    // Pagination identifier from the previous API call response.  Not needed for page one.
    $paginationIdentifier = 'eyJsY___';

    $getParams = array();
    $getParams['user_id'] = $userId;
    $getParams['start_time'] = $startTime;
    $getParams['end_time'] = $endTime;
    $getParams['statuses'] = $statusesArray;
    $getParams['meta_properties'] = $metaPropertiesArrayJsonStr;
    $getParams['limit'] = $limit;
    $getParams['pagination_identifier'] = $paginationIdentifier;

    $response = $transactionsService->getList($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $balancesService = $ostObj->services->balances;
    

    // Mandatory API parameters

    // UserId for whom balance needs to be fetched.
    $userId = 'c2c6___';
    $getParams = array();
    $getParams['user_id'] = $userId;

    $response = $balancesService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $recoveryOwnersService = $ostObj->services->recoveryOwners;
    

    // Mandatory API parameters

    // UserId for whom recovery details needs to be fetched.
    $userId = 'c2c___';

    // Recovery address of user.
    $recoveryOwnerAddress = '0xe37___';

    $getParams = array();
    $getParams['user_id'] = $userId;
    $getParams['recovery_owner_address'] = $recoveryOwnerAddress;

    $response = $recoveryOwnersService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $tokensService = $ostObj->services->tokens;
    

    /* Mandatory API parameters */
    // No mandatory parameters.

    $getParams = array();
    $response = $tokensService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $chainsService = $ostObj->services->chains;
    

    // Mandatory API parameters

    // ChainId for which details needs to be fetched. Only origin chainId and OST-specific auxiliary chainIds are allowed.
    $chainId = '2000';

    $getParams = array();
    $getParams['chain_id'] = $chainId;
    $response = $chainsService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $baseTokensService = $ostObj->services->baseTokens;
    

    /* Mandatory API parameters */
    // No mandatory parameters.

    $getParams = array();
    $response = $baseTokensService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $webhooksService = $ostObj->services->webhooks;
    

    // Mandatory API parameters

    // Array of topics.
    $topicParams = array("transactions/initiate", "transactions/success");

    // URL where you want to receive the event notifications.
    $url = 'https://www.testingWebhooks.com';

    // Optional API parameters

    // Status of a webhook. Possible values are 'active' and 'inactive'.
    $status = 'active';

    $createParams = array();
    $createParams['topics'] =  $topicParams;
    $createParams['url'] =  $url;
    $createParams['status'] =  $status;

    $response = $webhooksService->create($createParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    // Mandatory API parameters

    // Array of topics.
    $topicParams = array("transactions/initiate", "transactions/success", "transactions/failure");

    // Unique identifier for a webhook.
    $webhookId = 'a743___';

    // Optional API parameters

    // Status of a webhook. Possible values are 'active' and 'inactive'.
    $status = 'active';

    $updateParams = array();
    $updateParams['webhook_id'] = $webhookId;
    $updateParams['topics'] =  $topicParams;
    $updateParams['status'] =  $status;

    $response = $webhooksService->update($updateParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    // Mandatory API parameters

    // Unique identifier for a webhook.
    $webhookId = 'a743___';

    $getParams = array();
    $getParams['webhook_id'] = $webhookId;

    $response = $webhooksService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    // Mandatory API parameters
    // No mandatory parameters.

    // Optional API parameters

    // Limit.
    $limit = 10;

    // Pagination identifier from the previous API call response.  Not needed for page one.
    $paginationIdentifier = 'eyJwY___';

    $getParams = array();
    $getParams['limit'] = $limit;
    $getParams['pagination_identifier'] = $paginationIdentifier;

    $response = $webhooksService->getList($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    // Mandatory API parameters

    // Unique identifier for a webhook.
    $webhookId = 'a743___';

    $deleteParams = array();
    $deleteParams['webhook_id'] = $webhookId;

    $response = $webhooksService->delete($deleteParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    // Webhook data obtained.
    $webhookEventData = {"id":"54e3cd1c-afd7-4dcf-9c78-137c56a53582","topic":"transactions/success","created_at":1560838772,"webhook_id":"0823a4ea-5d87-44cf-8ca8-1e5a31bf8e46","version":"v2","data":{"result_type":"transaction","transaction":{"id":"ddebe817-b94f-4b51-9227-f543fae4715a","transaction_hash":"0x7ee737db22b58dc4da3f4ea4830ca709b388d84f31e77106cb79ee09fc6448f9","from":"0x69a581096dbddf6d1e0fff7ebc1254bb7a2647c6","to":"0xc2f0dde92f6f3a3cb13bfff43e2bd136f7dcfe47","nonce":3,"value":"0","gas_price":"1000000000","gas_used":120558,"transaction_fee":"120558000000000","block_confirmation":24,"status":"SUCCESS","updated_timestamp":1560838699,"block_timestamp":1560838698,"block_number":1554246,"rule_name":"Pricer","meta_property":{},"transfers":[{"from":"0xc2f0dde92f6f3a3cb13bfff43e2bd136f7dcfe47","from_user_id":"acfdea7d-278e-4ffc-aacb-4a21398a280c","to":"0x0a754aaab96d634337aac6556312de396a0ca46a","to_user_id":"7bc8e0bd-6761-4604-8f8e-e33f86f81309","amount":"112325386","kind":"transfer"}]}}}

    // Get webhoook version from webhook events data.
    $version = "v2";

    // Get ost-timestamp from the response received in event.
    $requestTimestamp = '1559902637';

    // Get signature from the response received in event.
    $signature = '2c56c143550c603a6ff47054803f03ee4755c9c707986ae27f7ca1dd1c92a824';

    $webhookSecret = 'mySecret';

    $params = array();
    $params["version"] = $version;
    $params["stringified_data"] = json_encode($webhookEventData);
    $params["request_timestamp"] = $requestTimestamp;
    $params["signature"] = $signature;
    $params["webhook_secret"] = $webhookSecret;

    $response = $webhooksService->verifySignature($params);
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $redeemableSkusService = $ostObj->services->redeemableSkus;
    

    // Mandatory API parameters

    // Fetch details of following redeemable sku.
    $redeemableSkuId = '1';
    
    $getParams = array();
    $getParams['redeemable_sku_id'] = $redeemableSkuId;
    $response = $redeemableSkusService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    // Mandatory API parameters
    // NOTE: No mandatory parameters.
  
    // Optional API parameters
  
    // Limit.
    $limit = 10;

    // Array of redeemable SKU ids.
    $redeemableSkuIds =  array('1', '2');

    // Pagination identifier from the previous API call response.  Not needed for page one.
    $paginationIdentifier = 'eyJsY___';
 
    $getParams = array();
    $getParams['redeemable_sku_ids'] = $redeemableSkuIds;
    $getParams['limit'] = $limit;
    $getParams['pagination_identifier'] = $paginationIdentifier;
    
    $response = $redeemableSkusService->getList($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    $redemptionsService = $ostObj->services->redemptions;
    

    // Mandatory API parameters
  
    // UserId of user for whom redemption details needs to be fetched.
    $userId = 'ee8___';
  
    // Unique identifier of the redemption of user.
    $redemptionId = 'aa___';
    
    $getParams = array();
    $getParams['user_id'] = $userId;
    $getParams['redemption_id'] = $redemptionId;
    $response = $redemptionsService->get($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    

    // Mandatory API parameters
    $userId = 'ee89___';
    
    // Optional API parameters
    
    // Limit.
    $limit = 10;
  
    // Array of user redemption uuids.
    $redemptionIds = array('a743___', 'a743___');
    
    // Pagination identifier from the previous API call response.  Not needed for page one.
    $paginationIdentifier = 'eyJsY___';
      
    $getParams = array();
    $getParams['user_id'] = $userId;
    $getParams['redemption_ids'] = $redemptionIds;
    $getParams['limit'] = $limit;
    $getParams['pagination_identifier'] = $paginationIdentifier;
    
    $response = $redemptionsService->getList($getParams)->wait();
    echo json_encode($response, JSON_PRETTY_PRINT);
    
bash
    curl -sS https://getcomposer.org/installer | php
bash
    php composer.phar