# Node.js SDK with CJS

## 1.  Installing node-SDK package

```bash
npm install @hypertestco/node-sdk --save-exact 
```

## 2.  Initializing sdk

#### &#x20;2.1 Adding SDK in code and Initialize&#x20;

* Initalize hypertest node sdk.

{% hint style="warning" %}
This needs to happen as early in your app as possible. Make sure no require/import call is made before these lines
{% endhint %}

```typescript
// process.env.APPLY_HT_OTEL_PATCH = 'yes'; // Set this env only if opentelemetry is already being used in your application.
process.env.HT_MODE = process.env.HT_MODE || 'RECORD'; // TODO: REMOVE THIS LINE BEFORE DEPLOYING TO PRODUCTION

import * as htSdk from '@hypertestco/node-sdk'; // for esm/ts
// const htSdk = require('@hypertestco/node-sdk'); // for commonJS
htSdk.initialize({
    apiKey: '<your-api-key>',
    serviceId: '<your-service-identifier-from-dashboard>',
    serviceName: '<organizationName:service-name>',
    exporterUrl: '<hypertest-logger-url>',
    // ignoredHostsForHttpReqs: ['abc.xyz.com', /^\d+\.abcd\.co(m|)$/],
    // disableInstrumentations: [] // htSdk.HtInstrumentations enum 
});

// No imports or require calls should be made before htSdk.initialize is called. Fight your instincts and your linters for this :)
```

#### 2.2 Mark app as ready

Call the markAppAsReady method when the app is ready to receive traffic, it indicates that tests can be started (This is important ONLY FOR REPLAY mode).

<pre class="language-javascript"><code class="lang-javascript">// Rest Application code...
// add htSdk.markAppAsReady(); when your app is ready to accept requests
<strong>app.listen(3000, () => {
</strong>  console.log(`Listening for requests on http://localhost:3000`);
  // Indicates the App has booted up successfully for REPLAY And Tests can be started
  htSdk.markAppAsReady();
});
</code></pre>

**2.3 Enable Test Creation**\
\
To enable hypertest, set the `HT_MODE` env variable to `RECORD` and start your app

```javascript
// Set this on top inside your JS app
process.env.HT_MODE = process.env.HT_MODE || 'RECORD';
```

OR while booting&#x20;

{% code fullWidth="false" %}

```bash
HT_MODE=RECORD node my-app.js

OR

export HT_MODE=RECORD
node my-app.js
```

{% endcode %}

#### 2.4 Set env if opentelemetry is already being used.

```javascript
// DO THIS ONLY IF YOU ARE USING OPENTELEMETRY ALREADY
// Set this on top inside your JS app
process.env.APPLY_HT_OTEL_PATCH = 'yes'; // Set this env only if opentelemetry is already being used in your application.
```

## 3. Verifying traffic is captured

Start sending http requests on your app running with hypertest sdk.

You should start seeing requests under All requests section on the dashboard

<figure><img src="/files/krogi7xn6W8mvMdR4l2e" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: 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:

```
GET https://docs-v2.hypertest.co/setup-guide/readme/integrate-sdk/node.js/integrate-nodejs-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
