> For the complete documentation index, see [llms.txt](https://docs.serphouse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.serphouse.com/official-sdks/node.js-sdk.md).

# Node.js SDK

The official Node.js SDK makes it easy to integrate the SERPHouse API into any Node.js application.

Instead of manually creating HTTP requests, the SDK handles authentication, request formatting, and response parsing so you can focus on building your application.

The SDK provides access to:

* Google SERP API
* Bing SERP API
* Yahoo SERP API
* Domains
* Languages
* Locations
* Account information

<table data-view="cards"><thead><tr><th></th><th data-hidden data-card-cover data-type="image">Cover image</th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td>SERPHouse Node.js SDK on GitHub</td><td><a href="/files/Xq0oyQfrev32xggtwIRe">/files/Xq0oyQfrev32xggtwIRe</a></td><td><a href="https://github.com/SERPHouse/serphouse-nodejs">https://github.com/SERPHouse/serphouse-nodejs</a></td></tr><tr><td>SERPHouse Node.js on NPM</td><td><a href="/files/kgutwlcCSn6IJyOkJPDM">/files/kgutwlcCSn6IJyOkJPDM</a></td><td><a href="https://www.npmjs.com/package/@serphouse/serphouse-nodejs">https://www.npmjs.com/package/@serphouse/serphouse-nodejs</a></td></tr></tbody></table>

***

### Requirements

* Node.js 16+
* npm or yarn

***

### Installation

Install the SDK using npm.

```bash
npm install serphouse
```

or with Yarn

```bash
yarn add serphouse
```

***

### Get Your API Key

Create a SERPHouse account and generate an API key from your dashboard.

***

### Initialize the Client

```javascript
const { SERPHouseClient } = require("serphouse");

const client = new SERPHouseClient({
    apiKey: process.env.SERPHOUSE_API_KEY,
});
```

Using environment variables is recommended.

```bash
SERPHOUSE_API_KEY=your_api_key
```

***

### Quick Start

Perform a live Google search.

```javascript
const response = await client.serp.live({
    data: {
        q: "Coffee",
        domain: "google.com",
        lang: "en",
        loc: "Texas, United States",
        device: "desktop",
        serp_type: "web",
    },
});

console.log(response);
```

***

### Available Services

The SDK exposes the following services.

| Service            | Description                    |
| ------------------ | ------------------------------ |
| `client.serp`      | Google, Bing & Yahoo SERP APIs |
| `client.domains`   | Supported domains              |
| `client.languages` | Supported languages            |
| `client.locations` | Search locations               |
| `client.account`   | Account information            |

***

### SERP API

#### Live Search

Retrieve search results immediately.

```javascript
const response = await client.serp.live({
    data: {
        q: "Coffee",
        domain: "google.com",
        lang: "en",
        device: "desktop",
        serp_type: "web",
        loc: "Texas, United States",
        page: 1,
        num_result: 10,
    },
});
```

***

#### Schedule a Search

Schedule searches for asynchronous processing.

```javascript
const response = await client.serp.schedule({
    data: [
        {
            q: "Coffee",
            domain: "google.com",
            lang: "en",
            device: "desktop",
            serp_type: "web",
            loc: "Texas, United States",
            postback_url: "https://example.com/postback",
            pingback_url: "https://example.com/pingback",
        },
    ],
});
```

***

#### Check Search Status

```javascript
const response = await client.serp.check(searchId);
```

***

#### Get Search Results

```javascript
const response = await client.serp.get(searchId);
```

Retrieve raw HTML.

```javascript
const response = await client.serp.get(searchId, "html");
```

***

### Domains

Retrieve all supported search domains.

```javascript
const response = await client.domains.list();
```

***

### Languages

Retrieve supported languages.

```javascript
const response = await client.languages.list();
```

Retrieve Bing languages.

```javascript
const response = await client.languages.list({
    type: "bing",
});
```

***

### Locations

Search available locations.

```javascript
const response = await client.locations.search({
    q: "Texas",
});
```

***

### Account

Retrieve account details.

```javascript
const response = await client.account.info();
```

***

### Error Handling

Handle API errors using `try...catch`.

```
try {    const response = await client.serp.live({        data: {            q: "Coffee",        },    });    console.log(response);} catch (error) {    console.error(error.message);}
```

***

### Using Async/Await

All SDK methods return Promises.

```javascript
try {
    const response = await client.serp.live({
        data: {
            q: "Coffee",
        },
    });

    console.log(response);
} catch (error) {
    console.error(error.message);
}
```

***

### Environment Variables

Store your API key securely.

```bash
SERPHOUSE_API_KEY=your_api_key
```

```javascript
require("dotenv").config();

const { SERPHouseClient } = require("serphouse");

const client = new SERPHouseClient({
    apiKey: process.env.SERPHOUSE_API_KEY,
});
```

***

### Complete Example

```javascript
require("dotenv").config();

const { SERPHouseClient } = require("serphouse");

const client = new SERPHouseClient({
    apiKey: process.env.SERPHOUSE_API_KEY,
});

async function main() {
    const results = await client.serp.live({
        data: {
            q: "Coffee",
            domain: "google.com",
            lang: "en",
            device: "desktop",
            serp_type: "web",
        },
    });

    console.log(results);
}

main();
```

***

### Support

If you encounter any issues while installing, configuring, or using the SERPHouse Node.js SDK, we're here to help.

* Contact the SERPHouse support team for assistance.
* Report bugs or request features by opening an issue on the GitHub repository.

GitHub Repository:

<https://github.com/SERPHouse/serphouse-nodejs>
