Announcement

Collapse
No announcement yet.

Node API Sample with HMAC and Timestamp

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Node API Sample with HMAC and Timestamp

    Not sure if this is needed or not but I was struggling with the API implementation and I have cobbled together some examples from various points in the documentation. I have a few examples but I will post one working example of one endpoint. I haven't seen the actual syntax for the parameters so I modified thru trial and error. For example the function docs list the following:

    ProductList_Load_Query

    But to access it in the call it was

    var request = new api.requests.ProductListLoadQuery(client);

    The options list also has to be in a specific order. For example:

    request.setFilters([{"Customer_ID":"int"}])
    .setCount(100)
    .setOffset(0)

    Will work for the get orders endpoint but if I try to set Offset before Count it will not work. The following example is a product export. I'll leave the assignments for account specific values but remove the private data.


    const express = require('express');
    const router = express.Router();
    const api = require('merchantapi');
    const MyApiToken = '{your_api_token}';
    const MySigningKey = '{your_signing_key}';
    const algorithm = 'sha256';
    const options = {
    require_timestamps: true,
    signing_key_digest: api.Client.SIGN_DIGEST_SHA256,
    default_store_code: 'test',
    Miva_Request_Timestamp : Math.floor(Date.now() / 1000)
    }
    const client = new api.Client( 'https://{your_url}/mm5/json.mvc', MyApiToken, MySigningKey, options );
    /*
    Product export route, requires no input parameters
    */
    router.get('/productexport',(req,res)=>{
    var request = new api.requests.ProductListLoadQuery(client);
    request.setCount( 100 ); // how many records to load
    request.setOffset( 0 ); // offset of records to load
    request.setStoreCode( 'test' ); // you can optionally set the store code here if you did not pass default_store_code in the client options.

    // apply filtering with the filter expression features:
    var filters = request.filterExpression();
    filters.isTrue( 'active' );
    request.setFilters( filters );
    request.send( function( error, response ){
    if (error){
    console.log(error);
    return;
    }
    else if ( !response.isSuccess() ){
    console.log('Load Products Error', response.getErrorCode(), response.getErrorMessage());
    }
    var productResults = response.getProducts();
    //loop for testing not neccdessarily needed
    response.getProducts().forEach((product)=>{
    console.log(product.getCode());
    });
    res.send(productResults);
    });
    });

    Basic but it works and it is the result of about 4 different sources.


  • We also have examples up on github

    https://github.com/mivaecommerce/api...aster/examples

    Have you looked though these yet?
    Brennan Heyde
    VP Product
    Miva, Inc.
    [email protected]
    https://www.miva.com

    Comment


    • I have tried the Nodejs SDK and PHP SDK they don't work.
      The issue is returning simple HTML Admin login page instead of JSON response.
      SDK shows an error: Error Decoding JSON: Syntax error

      Can you check this issue?

      The PHP code:
      <?php

      require_once __DIR__ . '/vendor/autoload.php';

      use MerchantAPI\Client;
      use MerchantAPI\ClientException;
      use MerchantAPI\Request\ProductListLoadQuery;
      use MerchantAPI\Request\ProductImageAdd;

      $config = json_decode(file_get_contents(__DIR__ . '/config.json'));

      $client = new Client(
      $config->url,
      $config->token,
      $config->key,
      [
      'require_timestamp' => true,
      'signing_key_digest' => Client::SIGN_DIGEST_SHA256,
      'default_store_code' => 'MYSTORE',
      'curl_options' => []
      ]
      );

      $reqProductList = new ProductListLoadQuery;
      $reqProductList->setFilters(
      $reqProductList->filterExpression()->equal('code', 'SOMEPROD')
      );

      try {
      $resProductList = $client->send($reqProductList);
      } catch (ClientException $e) {
      printf("Error: %s\n", $e->getMessage());
      exit;
      }


      Comment


      • The code looks correct. What value are you providing for the endpoint url?

        Comment

        Working...
        X

        This website uses cookies to identify visitors, track visitors to our website, store login session information and to remember your user preferences. By continuing to use this site you agree to our use of cookies. Learn More.

        This website uses cookies. By continuing to use this site you agree to our use of cookies. Learn More.

        Accept