> ## Documentation Index
> Fetch the complete documentation index at: https://gologin.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Node.js SDK

> Chạy profile Gologin bằng lập trình với Node.js SDK

### Cài đặt

```bash theme={null}
npm i gologin
```

### Cách dùng cơ bản

```javascript theme={null}
import GoLogin from 'gologin';

const GL = new GoLogin({
  token: 'your-api-token',
  profile_id: 'your-profile-id',
});

const { status, wsUrl } = await GL.start();
// Connect Puppeteer or Playwright using wsUrl
// ... your automation code ...
await GL.stop();
```

### Các tham số chính

| Tham số | Mặc định | Mô tả |
| - | - | - |
| `token` | bắt buộc | API token Gologin của bạn |
| `profile_id` | bắt buộc | ID của profile cần khởi chạy |
| `executablePath` | tự động | Đường dẫn đến tệp thực thi của trình duyệt Orbita |
| `uploadCookiesToServer` | `false` | Tải cookie lên đám mây khi dừng profile. Đặt thành `true` nếu bạn cần giữ phiên đăng nhập trên nhiều thiết bị. |
| `checkBrowserUpdate` | `true` | Kiểm tra bản cập nhật Orbita cho phiên bản của profile này khi khởi chạy. Đặt thành `false` để giữ nguyên phiên bản hiện tại. |
| `autoUpdateBrowser` | `true` | Tự động tải về các phiên bản Orbita mới. Đặt thành `false` để tránh các bản cập nhật ngoài ý muốn. |
| `extra_params` | `[]` | Các cờ Chromium bổ sung được truyền vào trình duyệt |

### Dừng profile

```javascript theme={null}
// Stop and sync to cloud (default)
await GL.stop();

// Stop without syncing any data to cloud
await GL.stop({ posting: false });
```

Lưu ý: `posting: false` ngăn dữ liệu profile được tải lên máy chủ Gologin. Hãy dùng tùy chọn này khi chạy trên máy chủ mà bạn không muốn đồng bộ lên đám mây.

### Quản lý phiên bản trình duyệt Orbita

```javascript theme={null}
import { checkAndDownloadBrowserByOpts } from 'gologin';

// Pre-download the latest Orbita version
await checkAndDownloadBrowserByOpts({ lastActualCount: 1 });

// Pre-download specific versions
await checkAndDownloadBrowserByOpts({ majorVersions: [142, 143] });
```

Hãy dùng `checkAndDownloadBrowserByOpts` trước khi khởi chạy nhiều profile song song để tránh nhiều lượt tải xuống diễn ra cùng lúc.

### Chạy hơn 100 profile song song

Khi chạy nhiều profile đồng thời trên máy chủ:

```javascript theme={null}
// Use a queue to limit concurrent launches
const CONCURRENCY = 10;
const queue = [...profileIds];
const active = new Set();

async function launchProfile(profileId) {
  const gl = new GoLogin({ token, profile_id: profileId });
  try {
    const { wsUrl } = await gl.start();
    // ... your automation ...
  } finally {
    await gl.stop();
  }
}

async function processQueue() {
  while (queue.length > 0) {
    while (active.size >= CONCURRENCY) {
      await new Promise(r => setTimeout(r, 1000));
    }
    const id = queue.shift();
    active.add(id);
    launchProfile(id).finally(() => active.delete(id));
  }
}
```

**Lưu ý quan trọng khi dùng máy chủ:**

* Giới hạn số profile khởi chạy đồng thời (nên bắt đầu với 10-20)
* Tải trước các phiên bản Orbita trước khi khởi chạy
* Trên Linux: cài đặt Xvfb và đặt `DISPLAY=:99`
* Tăng giới hạn file descriptor: `ulimit -n 65536`
* Theo dõi RAM: mỗi profile sử dụng khoảng 300-500 MB

### Gỡ lỗi

Đặt biến môi trường để bật ghi log chi tiết:

```bash theme={null}
DEBUG=gologin node your-script.js
```

### GitHub và Changelog

* Mã nguồn: [https://github.com/gologinapp/gologin](https://github.com/gologinapp/gologin)
* [Changelog](https://gologin.com/docs/vi/api-reference/sdks/nodejs-changelog.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.