# Javascript SDK

## Prerequisites

An API key is required for connecting to our REST API or equivalent SDKs. Get a free API key on our website home page at [cros.one](https://cros.one/).

### Technical Requirements

* Node.js >= 16

### How to Install

Install the Cros SDK via npm:

```sh
npm install cros-sdk
```

### Configuration

Provide your API key for authentication. This can be set as an environment variable:

```javascript
process.env.CROS_API_KEY = "your-api-key";
```

Alternatively, pass the API key directly to the CrosClient:

```javascript
import { CrosClient } from "cros-sdk";

const client = new CrosClient({ apiKey: "your-api-key" });
```

### Using the SDK

#### Start a Session

Start a new session:

```javascript
const session = await client.startSession({ keepAlive: true });
console.log("Session ID:", session.sessionId);
```

#### Close a Session

Close the current session:

```javascript
const response = await client.closeSession();
console.log("Session closed:", response.sessionId);
```

#### Observe a Webpage

Observe the state of a webpage:

```javascript
const observation = await client.observe("https://example.com");
console.log("Page Title:", observation.title);
console.log("Available Actions:", observation.actions);
```

**Example Output:**

```plaintext
Flight Search
* I1: Enter departure location (departureLocation: "San Francisco")
* I3: Select departure date (departureDate)
* I6: Select trip type (tripType: "round-trip", allowed: ["round-trip", "one-way", "multi-city"])
* B3: Search flight options with current filters
```

#### Step Through Actions

Execute an action on the observed page:

```javascript
const actionId = "navigate";
const observation = await client.step(actionId, "https://another-example.com");
console.log("New Page Title:", observation.title);
```

#### Scrape Data

Scrape data from a page:

```javascript
const observation = await client.scrape("https://example.com");
console.log("Scraped Data:", observation.data);
```

**Example Output:**

```plaintext
Flight Search inputs
- Where from?: Paris
- Where to?: London
- Departure: Tue, Jan 14

Flight Search Results
20 of 284 results returned.
They are ranked based on price and convenience

| Airline       | Departure  | Arrival  | Duration   | Stops     | Price |
|--------------|------------|----------|------------|-----------|-------|
| easyJet      | 10:15 AM   | 10:35 AM | 1 hr 20 min| Nonstop   | $62   |
| Air France   | 4:10 PM    | 4:35 PM  | 1 hr 25 min| Nonstop   | $120  |
```

### Managed API Services

We offer managed cloud browser sessions with the following premium add-ons:

* **Authentication**: Built-in authentication for secure workflows.
* **Caching**: Fast responses with intelligent caching.
* **Action Permissions**: Control over sensitive actions.

Request access to a set of API keys on [Cros](https://cros.one/).
