Sample source using Admin API

  • We provide sample references so that you can get started easily at the beginning of your app development.
  • Because the sample reference is made up of only the basic required codes, it is available for reference only.
  • The sample reference source code provides code written on a php basis.

Basic structure

Below is the basic structure for getting an Access Token and the result data by calling the API.

Sample source code for API call using a token

1. Shopping mall operator runs the app

  • When the shopping mall operator runs the app, it will be launched for the first time with the app URL path you entered when creating the app in the Developer Center.
  • The sample source below is the page shown when the app is run for the first time.
Sample example <html><body>- app execution complete -<p> <a href="{https://The development center Redirect URL(s)(URL no.2)}"> Get API credentials</a> </p> <p> <a href="{https://(URL no.4) }">Call API</a> </p></body></html>
  • - When the app is executed, the shopping mall‘s information and its operator that executed the app is sent in the format below.

https://{{AppUrl}}/?is_multi_shop={{multiple shopping malls}}&lang={{shopping mall language}}&mall_id={{mall ID}}&shop_no={{shop_no}}& timestamp= {{timestamp}}&user_id={{login ID}}&user_name={{login user name}}&user_type={{user type}}&hmac={{verification key}}

2. Requesting permission from the shopping mall operator to approve the API call

  • This is the step of getting the scope from the store owner to access the API call (issuing an Access Token) and install the app.
< ? php $sMallID = '{mall ID}'; $sClientID = '{client ID (app key)}'; $sAuthCodeReceiveUrl = 'url (URL no.3) to process the responded authentication code registered at the {https://Developer Center Redirect URL(s))}' use https:// $sScope = 'mall.write_product, mall.read_store, {additional scope ..}'; // See API Guide Scope Guide $aState = array( mall_id' => $sMallID, // fixed '{Arbitrary value a}' => '{arbitrary value b}' // add any value required for processing after issuing code if necessary ); // common below $sAuthCodeRequestUrl = sprintf("https://%s.cafe24api.com/api/v2/oauth/authorize?", $sMallID); $aRequestData = array( 'response_type'=>'code', 'client_id'=>$sClientID, 'state'=> base64_encode(json_encode($aState)), 'redirect_uri'=> $sAuthCodeReceiveUrl, 'scope'=> $sScope, ); $sUrl = $sAuthCodeRequestUrl . http_build_query($aRequestData); header('Location: ' . $sUrl);
  • 1) '$sMallID': Shopping Mall ID
  • Enter the shopping mall ID for which the API authorization is requested.
  • In the actual service stage, the shopping mall ID with the app installed is sent as a parameter to the app (please refer to article 1, shopping mall and operator information below)
- Information of shopping mall operator sent to the app

https: // {{AppUrl}} /? is_multi_shop = {{multiple shopping malls}} & lang = {{shopping mall language}} & mall_id = {{mall ID}} & shop_no = {{shop_no}} × tamp = {{timestamp} } & user_id = {{login ID}} & user_name = {{login user name}} & user_type = {{user type}} & hmac = {{verification key}}


- Source code under development < ? php $sMallID = '{mall ID`}'; $sClientID = '{client ID(App key)}'; $sAuthCodeReceiveUrl = 'url (URL no.3) to process the responded authentication code registered at the {https://Developer Center Redirect URL(s))}' use https:// $sScope = 'mall.write_product, mall.read_store, {additional scope ..}'; // See API Guide Scope Guide $aState = array( 'mall_id' => $sMallID, // fixed '{Arbitrary value a}' => '{arbitrary value b}' // add any value required for processing after issuing code if necessary );
  • 2) '$sClientID' : Client ID(App Key)
  • Enter your app key from the Development Center
- Source code under development < ? php $sMallID = '{mall ID}'; $sClientID = '{client ID(App key)}'; $sAuthCodeReceiveUrl = 'url (URL no.3) to process the responded authentication code registered at the {https://Developer Center Redirect URL(s))}' use https:// $sScope = 'mall.write_product, mall.read_store, {additional scope ..}'; // See API Guide Scope Guide $aState = array( 'mall_id' => $sMallID, // fixed '{Arbitrary value a}' => '{arbitrary value b}' // add any value required for processing after issuing code if necessary );
  • 3) '$sAuthCodeReceiveUrl': URL registered in Developer Center Redirect URL (s)
  • Enter the Redirect URL (s) to process the verification code input at the Development Center.
- Source code under development < ? php $sMallID = '{mall ID}'; $sClientID = '{client ID(App key)}'; $sAuthCodeReceiveUrl = 'url (URL no.3) to process the responded authentication code registered at the {https://Developer Center Redirect URL(s))}' use https:// $sScope = 'mall.write_product, mall.read_store, {additional scope ..}'; // See API Guide Scope Guide $aState = array( 'mall_id' => $sMallID, // fixed '{Arbitrary value a}' => '{arbitrary value b}' // add any value required for processing after issuing code if necessary );
  • You can use a comma (,) if you want to enter multiple Redirect Url (s) and receive a code-issuance token for all URLs entered.
- Example) If you want to enter multiple Redirect URL (s) (if the domain is aaa.com, bbb.com, ccc.com) < ? php $sMallID = '{mall ID}'; $sClientID = '{client ID (App key)}'; $sAuthCodeReceiveUrl = 'https://www.aaa.com,https://www.bbb.com,https://www.ccc.com'; // use https:// $sScope = 'mall.write_product, mall.read_store, {additional scope ..}'; // See API Guide Scope Guide $aState = array( 'mall_id' => $sMallID, // fixed '{Arbitrary value a}' => '{arbitrary value b}' // add any value required for processing after issuing code if necessary );
  • 4) '$sScope': Information on shopping mall API scope
  • You need to enter the scope of the shopping mall data from which the information was acquired using the API (for details, see the API Scope Guide))
- Source code under development < ? php $sMallID = '{mall ID}'; $sClientID = '{client ID (App key)}'; $sAuthCodeReceiveUrl = 'url (URL no.3) to process the responded authentication code registered at the {https: // Developer Center Redirect URL (s)) use // https: // $sScope = 'mall.write_product, mall.read_store, {additional scope ..}'; // See API Guide Scope Guide $aState = array( 'mall_id' => $sMallID, // fixed '{Arbitrary value a}' => '{arbitrary value b}' // add any value required for processing after issuing code if necessary );
  • - Input example: When the product classification (read right) and product (read + write right) are specified in the Developer Center and reflected in the source code
- Source code under development < ? php $sMallID = '{mall ID}'; $sClientID = '{client ID (App key)}'; $sAuthCodeReceiveUrl = 'https://www.aaa.com,https://www.bbb.com,https://www.ccc.com'; // use https:// $sScope = 'mall.write_product, mall.read_store, {additional scope ..}'; // See API Guide Scope Guide $aState = array( 'mall_id' => $sMallID, // fixed '{Arbitrary value a}' => '{arbitrary value b}' // add any value required for processing after issuing code if necessary );
  • * If API usage scopes are added / modified / deleted, you must have the Access Token reissued after agreeing to the changed scopes in the shopping mall for proper API calls.

3. Requesting a credential (Access Token)

  • This is the stage where the Access_Token is issued for an API call from the app.
< ? php $sClientId = '{client ID (App key)}'; $sClientSecret = '{client secret (app secret key)}'; $sThisUrl = 'current page url registered in {https: // Developer Center Redirect URL (s)'); // use https: // $sCode = $_REQUEST['code']; $sStatus = $_REQUEST['state']; // common below $aStatue = json_decode(base64_decode($sStatus), true); $aFields = array( 'grant_type' => 'authorization_code', 'code' => $sCode, 'redirect_uri' => $sThisUrl ); $oCurl = curl_init(); curl_setopt_array($oCurl, array( CURLOPT_URL => 'https://' . $aStatue['mall_id'] . '.cafe24api.com/api/v2/oauth/token', CURLOPT_POSTFIELDS => http_build_query($aFields), CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array( 'Authorization: Basic ' . base64_encode($sClientId . ':' . $sClientSecret) ) )); $sResponse = curl_ex ec($oCurl); // Try displaying to see if it is a normal token issuance response echo "<pre>"; print_r($sResponse);exit;

4. Authenticated API calls

  • This is the step of calling the API using the Access Token issued in the app.
< ? php $sAcctoken = '{value of Access Token issued from # 3}'; $sEndPointUrl = 'https://{Mall ID}.cafe24api.com/api/v2/admin/products'; // common below $oCurl = curl_init(); curl_setopt_array($oCurl, array( CURLOPT_URL => $sEndPointUrl, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array('Authorization: Bearer ' . $sAcctoken) )); $sResponse = curl_ex ec($oCurl); // Try displaying to see whether the API call is normal echo "<pre>"; print_r($sResponse);exit;

Sample source code for automatic script installation

  • If you need to insert a script into the front screen of the Cafe24 shopping mall platform, you can install (insert) the script on the desired screen easily and conveniently through the script API. The name of the screen to be installed can be checked through the screen code API.
  • The script reflected in the shopping mall front works by calling the script of the developer's runtime server.

Example) Source code to automatically install script on the front screen (provided as PHP source)

< ? php $sAcctoken = '{{value of Access Token issued}}'; $sEndPointUrl = 'https://{{Mall ID}}.cafe24api.com/api/v2/admin/scripttags'; $aPostData = array( "shop_no" => {{number of the shopping mall to install (shop_no)}}}, "request" => array( "client_id" => "{{App key(Client_id)}}", "src =" "{{script files to install (* .js)}}", "display_location" => "{screen code}}" // The screen on which the * .js is inserted. ) ); // Commonal code $oCurl = curl_init(); curl_setopt_array($oCurl, array( CURLOPT_URL => $sEndPointUrl, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => json_encode($aPostData), CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array('Authorization: Bearer ' . $sAcctoken, "Content-Type: application/json") )); $sResponse = curl_ex ec($oCurl); echo 'Successfully inserted'; exit;