Scripting example

Hello, i need scripting using JS example. I want to getTable getSpace getView etc. Where can i access examples?

let mainTable = base.getTable(cursor.activeTableId);

something like this

Hi, are you looking for JavaScript SDK usage examples based on the API? If so, please check this help document with a JS SDK quick start guide: https://bika.ai/help/guide/developer/openapi

import { Bika } from 'bika.ai';
const bika = new Bika({
    apiKey: '__PASTE_YOUR_API_TOKEN_FROM_USER_SETTING__',
    baseURL: 'https://bika.ai/api/openapi/bika',
});

async function fetchData() {
    try {
        const spaces = await bika.spaces.list();
        const space = spaces[0];
        const nodes = await space.nodes.list();
        const node = nodes[0];
        const database = await node.asDatabase();
        const records = await database.records.list();
        console.log('Records:', records);
    } catch (error) {
        console.error('Error fetching data:', error);
    }
}
fetchData();

If you need examples for using getTable, getSpace, and getView within Run Script Action, we’ll add relevant tutorial documentation shortly. Stay tuned!

Hi Kelvin, when i run test


where is the result?

Got it. Have you seen our Run Script documentation at Run Script | Bika.ai ? It includes basic JS/Python examples showing how to return values.

The Run Script Action doesn’t directly support getTable/getSpace/getView. You’ll need to combine actions to achieve this.

For example:

  1. Add a ​Find Records action before “Run Script” Action in your workflow to get database (run a test once to generate variables).

  1. In Run Script, use the ​slash “/” key to open the variable picker and select the output from “Find Records” action.

insert-variables-in-run-script-action

To get space lists:
Currently this requires calling our OpenAPI via fetch() in your script. See the API docs: https://bika.ai/help/openapi/bika#tag/Spaces/paths/~1v1~1spaces/get

1 Like

(async () => {
const url = ‘https://bika.ai/api/openapi/bika/v1/spaces’;
const apiKey = ‘TOKEN’; // Replace with your actual API key

const response = await fetch(url, {
method: ‘GET’,
headers: {
‘Authorization’: Bearer ${apiKey},
‘Content-Type’: ‘application/json’
}
});

if (!response.ok) {
throw new Error(HTTP error! Status: ${response.status});
}

const data = await response.json();
console.log(data);
})();

i am not getting responses. Any idea?

Hey there! Looks like there are some syntax issues in your code causing it to fail. You might want to check out the code example below to fix it. Also, two quick tips:

  1. When using Run Script, make sure to return a value at the end - that becomes your action’s output. Docs here: https://bika.ai/help/guide/automation/run-script-action
  2. console.log won’t show up in Automation Run History. For debugging, pack your variables into a JS object and return it to see the values. Hope this helps!
(async () => {
const url = 'https://bika.ai/api/openapi/bika/v1/spaces';
const apiKey = '{YOUR_API_TOKEN}';

const response = await fetch(url, {
    'method': 'GET',
    'headers': {
        'Authorization': "Bearer " + apiKey,
        'Content-Type': 'application/json'
    }
});

if (!response.ok) {
    throw new Error("HTTP error! Status:" + response.status);
}

return await response.json();
})();

Preview

Run successful at 2025-04-10 11:22:08

RUN_SCRIPT

Run Script

Output:{

success:

false

code:

401

message:

“Unauthorized”

data:{}

}

why i got this?

Looks like you forgot to replace the API key in the sample code! The “Unauthorized” error in your output basically means the system didn’t recognize your access credentials.