Skip to content
My Gold Team edited this page Sep 23, 2019 · 8 revisions

MyGold Client API Reference App

Simple examples to use the MyGold API.

MyGold API is currently hosted as AWS API GAteway Methods. Since the AWS API GAteway security require extensive signature computations and verifications We recommend using AWS API Gateway Client to consume these APIs.

  • You will get a short JSON snippet with all required IDs and credentials.
  • This snippet need to be used to update config/credentials.json
  • For API Reference please check this link
  • The example project is written in Javascript targetting nodejs runtime. It uses aws-sdk for authentication as well as communication with APIs.
  • The same techniques can be implemented in any programming language using many other appropriate client libraries capable of authenticated REST API Calls.

Quick Start

  1. Download and install latest nodejs for your target platform.
  2. Git clone this project to your computer
git clone https://github.com/goldsip/mfipartnerapi-example.git ./
  1. On the command line go to the folder where the project is cloned.

  2. Execute the following command to download and save all required nodejs dependencies.

    npm install  
    
  3. Update the config/credentials.json. You would have received a JSON snippet with your specific access credentials.


    Default "Credential Profile" used is "test" and will work correctly with your default credentials.json. You can setup multiple profiles if system has provided you more than one "credential". For example to manage Staging Vs. Production APIs. Profile switching is managed using an environment variable - mygold_stage . You can add this environment variable with appropriate profile name to use a different credential. How to setup environment variables

    #UNIX, bash shell
    #To use dev profile in credentials.json (UNIX, bash shell)
    mygold_stage=dev
    export mygold_stage
    
    #UNIX, bash shell
    #To use test profile in credentials.json (This is default)
    mygold_stage=test
    export mygold_stage
    

 {
     "dev":{

     },    
     "test":{
         "user":"AAAAAAA",
         "password":"BBBBBB",
         "userPool":"CCCCCCCC",
         "appClient":"DDDDDDD",
         "identityPool":"EEEEEEEE",
         "region":"FFFFFF",
         "basePath":"https://testapi.mygold.co.in/test/partners/XXXX"
     },
     "prod":{

     }
 }

If config/credentials.json does not exist in your cloned repository, create one. This file is excluded from the code repository because of the confidential nature of credential information. DO NOT PUBLISH your credentials.json to a public repository. Keep it safe and protected.


  1. You can now run a basic test case that will check
    1. Your configuration is updated correctly (config/credentials.json).
    2. Your credentials are correct and is usable with API server.
    3. You are able to use the APIs that you have access to.
    ~/src/mfipartnerapi-example$ node tests/testSetup.js 
    Authenticating with User Pool
    Getting temporary credentials
    OK - Good API Call    
    Expected output is : OK - Good API Call

If you face problems contact our technical team to help you

Usage

Authenticating and getting a client reference

authenticate.js uses the credentials and configuration data saved to config/credentials.json to initialize a client instance. This is returned through a callback.

//Config and Authentication library.
const authenticatiion = require('../auth/authenticate.js');

//
authenticatiion.authenticateClient(function (err, client) {
    if (client) {
        function callback(error, response, body) {
            if (!error && response.statusCode == 200) {
                console.log(JSON.stringify(body));
            }
        }
        //Now you can use the client to consume APIs.
    }
    else {
        console.error(err);
    }
})

Using client to access API Methods

Using the authenticated client to fetch data from REST API endpoint. Here the code here is using /branches api call to fetch back all existing branches inside your MFI organization.

    client
        .invokeApi(null, '/branches', 'GET')
        .then(function (result) {
            console.log(result.data)
        })
        .catch(function (result) {
            if (result.response) {
                console.dir({
                    status: result.response.status,
                    statusText: result.response.statusText,
                    data: result.response.data
                });
            } else {
                console.log(result.message);
            }
        });

Here the code here is using /branches api call to upload (POST) branches to your MFI organization.

    var _id = shortid.generate();
    /**
     * Generating random test data.
     */
    const _branches = [
        {extBranchId: `${_id}1`, name:`Branch - (${_id}1`  },
        {extBranchId: `${_id}2`, name:`Branch - (${_id}2`  },
        {extBranchId: `${_id}3`, name:`Branch - (${_id}3`  }
    ]
    client
        .invokeApi(null, '/branches', 'POST', {}, _branches)
        .then(function (result) {
            console.log(result.data)
        })
        .catch(function (result) {
            if (result.response) {
                console.dir({
                    status: result.response.status,
                    statusText: result.response.statusText,
                    data: result.response.data
                });
            } else {
                console.log(result.message);
            }
        });

Using client to Upload a document.

For certain entities there could be one or many binary attachments. For example a customer can have photo or scanned identity documents attached. You can use corresponding API calls to upload/retrive such documents.

Authorize secure upload of a customer's binary document such a photograpgh, or identification proof scan
to the storage. These uploads are a two step process. In the first step an authorization is completed. On successful authorization caller will receive an upload endpoint (URL). A 'PUT' operation is expected to this URL with the file binary data.

We highly recommend using the client library uploadclient.js for uploads. If you are rolling your own upload routine using this API the following parameter is important.

UploadDocRequest.type should be 'CUSTOMER_DOC'

Example

        let apiClient = await authenticatiion.authenticateClientAsync();                
        let result = await uploadclient.uploadClientDocument(apiClient,extCustomerId,uploadfilePath);
        console.log(result);

Authorize secure upload of a branch's binary document such an identification proof scan
to the storage. These uploads are a two step process. In the first step an authorization is completed. On successful authorization caller will receive an upload endpoint (URL). A 'PUT' operation is expected to this URL with the file binary data.

We highly recommend using the client library uploadclient.js for uploads. If you are rolling your own upload routine using this API the following parameter is important.

UploadDocRequest.type should be 'BRANCH_DOC'

Example

    let apiClient = await authenticatiion.authenticateClientAsync();                
    let result = await uploadclient.uploadBranchDocument(apiClient,extBranchId,uploadfilePath);
    console.log(result);    

Authorize secure upload of a agent's binary document such a photograpgh, or identification proof scan
to the storage. These uploads are a two step process. In the first step an authorization is completed. On successful authorization caller will receive an upload endpoint (URL). A 'PUT' operation is expected to this URL with the file binary data.

We highly recommend using the client library uploadclient.js for uploads. If you are rolling your own upload routine using this API the following parameter is important.

UploadDocRequest.type should be 'AGENT_DOC'

Example

    let apiClient = await authenticatiion.authenticateClientAsync();                
    let result = await uploadclient.uploadAgentDocument(apiClient,extAgentId,uploadfilePath);
    console.log(result);