# TypeScript & JavaScript SDK

Cài đặt SDK và tìm kiếm trong Node.js. Chọn TypeScript hay JavaScript; giữ API Chìa khóa trên máy chủ.

Tài liệu tham khảo chính điển: https://flashdata.dev/vi/docs/guides/flashdata-typescript
Xem lại: 2026-11-04 by FlashData

- [GitHub](https://github.com/flashdatadev/flashdata-typescript) — Nguồn và tham khảo SDK
- [Lấy khóa API](https://flashdata.dev/app/api-keys) — Tạo một phím trong bàn điều khiển

<a id="application"></a>

## Tạo yêu cầu Node.js đầu tiên

Cài đặt gói, đặt API phím và lựa chọn TypeScript hay JavaScript Bên dưới. Chạy mã này trên máy phục vụ của bạn hoặc trong một tập lệnh cục bộ.

- [SDK tham chiếu phương thức](https://github.com/flashdatadev/flashdata-typescript/blob/v1.1.0/docs/api-reference.md)
- [npm](https://www.npmjs.com/package/@flashdata-dev/sdk/v/1.1.0)

<a id="install"></a>

### Cài đặt SDK

Những ví dụ này được dùng Node.js 22.18+ để chạy TypeScript trực tiếp. Bắt đầu trong một thư mục rỗng, hoặc chọn dự án tồn tại nếu bạn có package.json.

#### Dự án mới

##### Cài đặt

```bash
npm init -y
npm install --save-exact @flashdata-dev/sdk@1.1.0
```


#### Dự án tồn tại

##### Cài đặt

```bash
npm install --save-exact @flashdata-dev/sdk@1.1.0
```


- [Cài đặt Node.js](https://nodejs.org/en/download)

<a id="key"></a>

### Xác thực

Tạo một phím bằng Google Search Cho phép hoặc sử dụng lại một khóa có thẩm quyền. Đặt FLASHDATA_API_KEY trong thiết bị cuối này, sử dụng dấu nhắc ẩn bên dưới; bỏ qua nếu môi trường của bạn đã cung cấp nó.

Ví dụ cho thấy rõ ràng chìa khóa. Tập tin .env không được tải bởi các tập lệnh này. Giữ phím trong cấu hình bên máy phục vụ khi tích hợp vào ứng dụng web.

#### Bash

##### Phím ẩn nhắc điều · Bash

```bash
# Bash: paste the key into a hidden prompt, not into this command.
read -r -s -p "FlashData API key: " FLASHDATA_API_KEY
printf '\n'
export FLASHDATA_API_KEY
```


#### PowerShell 7

##### Phím ẩn nhắc điều · PowerShell

```powershell
# PowerShell 7: paste the key into a hidden prompt.
$flashdataSecureKey = Read-Host "FlashData API key" -AsSecureString
$env:FLASHDATA_API_KEY = [System.Net.NetworkCredential]::new("", $flashdataSecureKey).Password
Remove-Variable flashdataSecureKey
```


- [Đăng ký](https://flashdata.dev/signup)
- [Mở API Keys](https://flashdata.dev/app/api-keys)
- [Mở Billing](https://flashdata.dev/app/billing)
- [Thiết lập tác nhân phiên tòa](https://flashdata.dev/vi/docs/guides/agent-onboarding/reference.md)

<a id="query"></a>

### Chạy tìm kiếm

Lưu tập tin đã chọn và chạy lệnh trong dự án này. Mỗi lần chạy một lần tìm kiếm kết quả là năm kết quả. Cả hai tập tin chạy trực tiếp với Node.js; không cần phải có bước xây dựng cho ví dụ này.

#### TypeScript

##### quickstart.mts

```typescript
import { FlashData, type GoogleSearchParams } from '@flashdata-dev/sdk';

const apiKey = process.env.FLASHDATA_API_KEY;
if (!apiKey) throw new Error('Set FLASHDATA_API_KEY in the server environment.');
const client = new FlashData({ apiKey });
const params: GoogleSearchParams = { num: 5 };
const response = await client.google.search({ query: 'battery recycling research', ...params });
if (response.status !== 'done') throw new Error(JSON.stringify(response));

for (const page of response.results) {
  for (const hit of page.organic ?? []) console.log(hit.title, hit.link);
}
console.log('Credits used:', response.usage.credits);
```

##### Chạy · Node.js 22.18+

```bash
node quickstart.mts
```


#### JavaScript

##### quickstart.mjs

```javascript
import { FlashData } from '@flashdata-dev/sdk';

const apiKey = process.env.FLASHDATA_API_KEY;
if (!apiKey) throw new Error('Set FLASHDATA_API_KEY in the server environment.');
const client = new FlashData({ apiKey });
const response = await client.google.search({ query: 'battery recycling research', num: 5 });
if (response.status !== 'done') throw new Error(JSON.stringify(response));

for (const page of response.results) {
  for (const hit of page.organic ?? []) console.log(hit.title, hit.link);
}
console.log('Credits used:', response.usage.credits);
```

##### Chạy

```bash
node quickstart.mjs
```



<a id="result"></a>

### Kiểm tra kết quả

Kiểm tra tình trạng tập lệnh đã được thực hiện và in tựa nguồn, URLs và dùng tín dụng. Kết quả hữu cơ rỗng là hợp lệ. Thí dụ xuất ra bên dưới minh họa hình dạng; các nguồn sống sẽ khác nhau.

| Tìm kiếm | Dùng |
| --- | --- |
| results[].organic[] | Google Search Đầu đề, liên kết và đoạn trích. Các nguồn khác có các loại hàng hóa riêng. |
| usage.credits | Tín dụng cho lời yêu cầu này; năm lần trở lại là một yêu cầu tìm kiếm. |

#### Đầu ra Ví dụ

```text
Battery recycling research https://example.org/recycling
Credits used: 0.02
```


<a id="common-tasks"></a>

## Thử một nguồn dữ liệu khác

Mỗi thẻ làm việc trong cả hai TypeScript và JavaScript. Thay thế tập tin từ bước 3Giữ nó mở rộng hay mở rộng ra và chạy cùng một mệnh lệnh. Mỗi lần chạy là một yêu cầu mới và cần có sự cho phép từ nguồn.

### Tin tức Google

Yêu cầu google_news. Đọc mục nhập bài trong results[].news[].

#### quickstart.mts / quickstart.mjs

```typescript
import { FlashData } from '@flashdata-dev/sdk';

const apiKey = process.env.FLASHDATA_API_KEY;
if (!apiKey) throw new Error('Set FLASHDATA_API_KEY first.');
const client = new FlashData({ apiKey });
const response = await client.google.news({
  query: 'battery recycling', num: 5,
});
if (response.status !== 'done') throw new Error(JSON.stringify(response));
console.log(response);
```


### YouTube Siêu dữ liệu

Yêu cầu youtube_metadata. Trả lại tựa đề, kênh, thời gian và các chi tiết khác.

#### quickstart.mts / quickstart.mjs

```typescript
import { FlashData } from '@flashdata-dev/sdk';

const apiKey = process.env.FLASHDATA_API_KEY;
if (!apiKey) throw new Error('Set FLASHDATA_API_KEY first.');
const client = new FlashData({ apiKey });
const response = await client.youtube.metadata({
  video_id: 'dQw4w9WgXcQ', include_formats: false,
});
if (response.status !== 'done') throw new Error(JSON.stringify(response));
console.log(response);
```


### Bản ghi YouTube

Yêu cầu youtube_transcript. Yêu cầu theo dõi tự động tiếng Anh. Văn bản vào results[].fullText Khi nào có đường ray.

#### quickstart.mts / quickstart.mjs

```typescript
import { FlashData } from '@flashdata-dev/sdk';

const apiKey = process.env.FLASHDATA_API_KEY;
if (!apiKey) throw new Error('Set FLASHDATA_API_KEY first.');
const client = new FlashData({ apiKey });
const response = await client.youtube.transcript({
  video_id: 'dQw4w9WgXcQ', language: 'en', auto: true,
});
if (response.status !== 'done') throw new Error(JSON.stringify(response));
console.log(response);
```


- [Mọi tham số nguồn](https://github.com/flashdatadev/flashdata-typescript/blob/v1.1.0/docs/sources/index.md)

<a id="typecheck"></a>

## Kiểm tra gõ một dự án TypeScript

Nút chạy ví dụ này bằng cách lột đồ loại. Để kiểm tra các kiểu mà không gửi yêu cầu, cài đặt TypeScript và chạy trình biên dịch --noEmit:

### Kiểm tra kiểu tùy chọn

```bash
npm install --save-dev typescript@~5.9.3 @types/node@^22
npx tsc quickstart.mts --module nodenext --target es2022 --strict --noEmit
```

- [Node.js TypeScript hỗ trợ](https://nodejs.org/api/typescript.html)

<a id="features"></a>

## Hỗ trợ SDK là gì?

| Dùng | Phương pháp |
| --- | --- |
| google / youtube | Tìm kiếm và thu thập dữ liệu cấu trúc với hỗ trợ nguồn đã gõ. Hãy tái sử dụng một khách hàng trong dịch vụ của anh. |
| youtube.download | Chuyển âm thanh hoặc video đến kho đám mây thông qua một công việc nền. Yêu cầu youtube_download Cho phép và lưu trữ cấu hình. |
| jobs / bulk | Lưu công việc đã gửi IDs và lấy kết quả đã hoàn tất. Đang đợi kết quả vào JavaScript; mode: 'async' Đang gửi một tác vụ nền của máy chủ. |
| account | Đọc sách, giữ thăng bằng, giá cả, giới hạn và sử dụng trước khi làm việc lặp lại. |
| respondWith / accept | Qua { respondWith: 'markdown' } là đối số thứ hai của phương pháp cho Markdown in response.content, hoặc { accept: 'text/plain' } cho một sợi dây. JSON Đối tượng là sự mặc định. |

- [Ví dụ tải về](https://github.com/flashdatadev/flashdata-typescript/blob/v1.1.0/examples/youtube_download.mjs)
- [Thiết lập lưu trữ](https://flashdata.dev/vi/docs/guides/youtube-download-guide/reference.md)
- [Định dạng phản hồi](https://github.com/flashdatadev/flashdata-typescript/blob/v1.1.0/docs/response-formats.md)

<a id="next"></a>

## Bước kế

- [Lưu kết quả và xử lý lỗi](https://flashdata.dev/vi/docs/guides/sdk-operations/reference.md)
- [Công việc và mẻ](https://github.com/flashdatadev/flashdata-typescript/blob/v1.1.0/docs/jobs.md)
- [Cấu hình khách](https://github.com/flashdatadev/flashdata-typescript/blob/v1.1.0/docs/client.md)
- [SDK tham chiếu phương thức](https://github.com/flashdatadev/flashdata-typescript/blob/v1.1.0/docs/api-reference.md)

<a id="errors"></a>

## Nếu một bước không thành công

| Vấn đề | Sửa |
| --- | --- |
| Gói hoặc chạy thời gian thiếu | Kiểm tra nút --version và cài đặtflashdata-dev/sdk trong dự án này. Giữ phần mở rộng .mjs được hiển thị bên trên. |
| 401 / FLASHDATA_API_KEY | Đặt một phím hợp lệ trong thiết bị cuối nơi bạn chạy ví dụ. |
| 403 | Bật Google Search Vì cái này API Chìa khóa. |
| 402 | Kiểm tra Billing hoặc tuyên bố một vụ thử nghiệm tác nhân kiệt sức. |
| 429 | Chờ Retry-After, sau đó giảm tốc độ yêu cầu. |
| Lỗi quá giờ hoặc máy chủ | Kiểm tra History trước khi phục hồi, có lẽ yêu cầu đầu tiên đã chạy rồi. |

- [Yêu cầu lịch sử](https://flashdata.dev/app/jobs)
- [Billing](https://flashdata.dev/app/billing)
- [SDK lỗi và phục hồi](https://github.com/flashdatadev/flashdata-typescript/blob/v1.1.0/docs/errors.md)
