> For the complete documentation index, see [llms.txt](https://developer.sahamati.org.in/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.sahamati.org.in/sahamatinet-poc/integration-steps/integration-with-router/sample-code-snippets/javascript.md).

# JavaScript

<details>

<summary>Without Router - Current Approach</summary>

```javascript
// Assuming this as previous logic without Router
const axios = require('axios');

const entityMetadataFromCR = {
    baseUrl: 'http://fip-1.dev.sahamati.org.in/fip-simulate',
    id: "FIP-SIMULATOR"
}

const getHttpConfig = ({ baseUrl, route, headers, data, methodType }) => {
    return { url: `${baseUrl}${route}`, headers: headers, data, methodType };
};

const executeDiscoveryRequest = () => {
    const route = '/v2/Accounts/discover';

    const config = getHttpConfig({ baseUrl: entityMetadataFromCR.baseUrl, route, headers: {}, data: {}, methodType: 'POST' });

    axios.request(config)
        .then(response => response.data)
        .catch(error => {
            console.error('Error making discovery request:', error.message);
            throw error;
        });

};

const performAnotherOperations = async (accountDiscoverResponse) => {
    console.log('Perform another operations with the response', accountDiscoverResponse);
}

executeDiscoveryRequest()
    .then(performAnotherOperations)
    .then(() => console.log('Account discovery successful.'))
    .catch(() => console.log('Account discovery failed.'))
    .finally(() => console.log('Account discovery completed.'));

```

</details>

<details>

<summary>With Router Integration</summary>

```javascript
// Changes to implementation to integrate with Router
const axios = require('axios');

// OLD START

const entityMetadataFromCR = {
    baseUrl: 'http://fip-1.dev.sahamati.org.in/fip-simulate',
    id: "FIP-SIMULATOR"
}

const getHttpConfig = ({ baseUrl, route, headers, data, methodType }) => {
    return { url: `${baseUrl}${route}`, headers: headers, data, methodType };
};

const executeDiscoveryRequest = () => {
    const route = '/v2/Accounts/discover';
    
    let config = getHttpConfig({ baseUrl: entityMetadataFromCR.baseUrl, route, headers: {}, data: {}, methodType: 'POST' });

    // add Sahamati configuration [Start]
    config = addSahamatiConfiguration({ config, route });
    // add Sahamati configuration [End]

    axios.request(config)
        .then(response => response.data)
        .catch(error => {
            console.error('Error making discovery request:', error.message);
            throw error;
        });
};

const performAnotherOperations = async (accountDiscoverResponse) => {
    console.log('Perform another operations with the response', accountDiscoverResponse);
}

executeDiscoveryRequest()
    .then(performAnotherOperations)
    .then(() => console.log('Account discovery successful.'))
    .catch(() => console.log('Account discovery failed.'))
    .finally(() => console.log('Account discovery completed.'));


// OLD END


// NEW START
  
const generateBase64EncodedJson = (json) => {
    return btoa(JSON.stringify(json));
}

const generateRequestMeta = (recipientId) => {
    const context = { "recipient-id": recipientId }
    return generateBase64EncodedJson(context);
}

const addSahamatiConfiguration = ({ config, route }) => {
    const routerUrl = 'http://api.dev.sahamati.org.in/fip-router';
    config.url = `${routerUrl}${route}`;
    config.headers["x-request-meta"] = generateRequestMeta(entityMetadataFromCR.id);
    return config;
}

// NEW END
```

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developer.sahamati.org.in/sahamatinet-poc/integration-steps/integration-with-router/sample-code-snippets/javascript.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
