LogoFreestyle

Run Code

Run code you didn't write

Simple Code Execution

Install the required dependencies

npm i freestyle-sandboxes
pnpm i freestyle-sandboxes
bun i freestyle-sandboxes
yarn add freestyle-sandboxes
pip install freestyle

Create a Freestyle Client

import { FreestyleSandboxes } from "freestyle-sandboxes";

const api = new FreestyleSandboxes({
  apiKey: process.env.FREESTYLE_API_KEY!, // make sure to set this
});
import freestyle

client = freestyle.Freestyle("YOUR_FREESTYLE_API_KEY") # make sure to set this

Run the code

import { FreestyleSandboxes } from "freestyle-sandboxes";

const api = new FreestyleSandboxes({
apiKey: process.env.FREESTYLE_API_KEY!, // make sure to set this
});

const code = `export default () => {
// calculate the factorial of 543
return Array.from({ length: 543 }, (_, i) => i + 1).reduce((acc, cur) => acc * cur, 1);
}`;

api.executeScript(code).then((result) => {
console.log("Result: ", result);
});
import freestyle
client = freestyle.Freestyle("YOUR_FREESTYLE_API_KEY") # make sure to set this

const code = """
export default () => {
// calculate the factorial of 543
return Array.from({ length: 543 }, (_, i) => i + 1).reduce((acc, cur) => acc * cur, 1);
}
"""

response = client.execute_script(code)

print(f"Result: {response.result}")

Advanced Code Execution

Custom Node Modules

import { FreestyleSandboxes } from "freestyle-sandboxes";

const api = new FreestyleSandboxes({
  apiKey: process.env.FREESTYLE_API_KEY!, // make sure to set this
});

api
  .executeScript(
    `import axios from 'axios';

    export default async () => {
      // Fetch data from an external API
      const res = await axios.get('https://jsonplaceholder.typicode.com/todos/1');
      return res.data;
    }`,
    {
      nodeModules: {
        axios: "0.21.1", // specify the version of the module you want to use
      },
    }
  )
  .then((result) => {
    console.log("Result: ", result);
  });
import freestyle

client = freestyle.Freestyle("YOUR_FREESTYLE_API_KEY")

result = client.execute_script(
    code="""

    import axios from 'axios';

    export default async () => {
        // Fetch data from an external API
        const res = await axios.get('https://jsonplaceholder.typicode.com/todos/1');
        return res.data;
    }
    """,
    freestyle.FreestyleExecuteScriptParamsConfiguration(
      node_modules={
        "axios": "0.21.1"  # specify the version of the module you want to use
      }
    }
  ),
)
print(f"Result: {result.result}")

This pattern can be used for any node modules, and can be used to connect to any API or service.

Custom Environment Variables

import { FreestyleSandboxes } from "freestyle-sandboxes";

const api = new FreestyleSandboxes({
  apiKey: process.env.FREESTYLE_API_KEY!, // make sure to set this
});

api
  .executeScript(`return process.env.SOME_ENV_VAR;`, {
    envVars: {
      SOME_ENV_VAR: "Hello, World!",
    },
  })
  .then((result) => {
    console.log("Result: ", result);
  });
import freestyle

    client = freestyle.Freestyle("YOUR_FREESTYLE_API_KEY")

    result = client.execute_script(
        code="""
        return process.env.SOME_ENV_VAR;
        """,
        freestyle.FreestyleExecuteScriptParamsConfiguration(
            env_vars={
                "SOME_ENV_VAR": "Hello, World!"
            }
        )
    )

    print(f"Result: {result.result}")

Environment variables are accessible via the process.env object in the code execution environment.

Running Code with AI

Check out our integrations — we have support for all major AI Agent frameworks. AI models today have gotten incredibly good at writing code, when you give your Agents the ability to run code the scope of problems they can solve. Specifically it makes, data integration and analytical questions. When connecting to an external tool you can build 20 different tools, or you can give your AI the docs and let it figure out how to connect — the ladder is much more adaptable.

Most people who use us start with the prebuilt AI integrations liners, but then move towards a more fine grained approach executing the code themselves with custom definitions.