# JavaScript

The OpenCGA JavaScript Client is provided as part of [JSorolla](https://github.com/opencb/jsorolla).\
Some examples of basic usage can be found in [examples](https://github.com/opencb/jsorolla/tree/develop/src/core/clients/opencga/examples) directory.

### Example:

#### Fetching the first 10 variants of the Study of interest using a token

```
import {OpenCGAClient} from "./opencga-client.js";

const HOST = "";      // add your host
const STUDY = "";     // add your study of interest
const TOKEN = "";     // add a valid token

const client = new OpenCGAClient({
    host: HOST,
    version: "v2",
    cookies: {active: false},
    token: TOKEN
});


(async () => {
    try {
        const restResponse = await client.variants().query({study: STUDY, limit:10});
        console.table(restResponse.getResults());
    } catch (response) {
        if (response instanceof RestResponse) {
            console.error(response.getEvents())
        } else {
            console.error(response)
        }
    }
})();
```

#### Fetching the first 10 variants of the Study of interest using OpenCGA credentials.

In this case an Opencga Session is created. The Opencga Study being used is the default one for the user.&#x20;

```
import {OpenCGAClient} from "./opencga-client.js";

const HOST = "";      // add your host
const STUDY = "";     // add your study of interest
const USERNAME = "";     // add your username
const PASSWORD = "";     // add your username

const client = new OpenCGAClient({
    host: HOST,
    version: "v2",
    cookies: {active: false}
});
(async () => {
    try {
        await client.login(USERNAME, PASSWORD)
        const session = await client.createSession();
        const restResponse = await session.opencgaClient.variants().query({limit:10, study: session.study.fqn});
        console.table(restResponse.getResults());

    } catch (e) {
        console.error(e)
    }
})();
```


---

# 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.opencga.opencb.org/develop-2/manual/using-opencga/client-libraries/javascript.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.
