# Playwright in the API Maker sandbox

> Override the sandbox Docker file, add Playwright as a sandbox dependency and generate a PDF from a web page inside the API Maker sandbox.

Source: https://docs.apimaker.dev/v1/docs/blog/General-Blogs/Playwright-in-sandbox.html

This blog describes the procedure to install and use **Playwright** within the **API Maker Sandbox Environment**.

---

## Overview

Playwright can be used inside the API Maker sandbox to perform browser-based automation such as:

- Page navigation
- PDF generation
- Web scraping
- Headless browser execution

---

## System Requirements

Ensure the API Maker environment meets the following minimum requirements:

- **Memory:** 2 GB RAM
- **Storage:** 40 GB disk space

---

## Step 1: Override the Default Sandbox Docker Image

Playwright requires system-level dependencies that are not present in the default sandbox image. To install them, the sandbox Dockerfile must be overridden.

### Instructions

- Navigate to **API Maker -> Sandbox Settings**
- Locate the **Dockerfile** configuration section
- Replace the existing content with the following Dockerfile

```dockerfile
FROM node:22-bookworm
WORKDIR /usr/src/app

RUN npm install -g pnpm@10.27.0
RUN apt-get update && apt-get install -y --no-install-recommends build-essential g++ make libc6 python3 python3-dev && rm -rf /var/lib/apt/lists/*

ARG A_DOCKERFILE_HASH
ENV A_DOCKERFILE_HASH=${A_DOCKERFILE_HASH}

ARG A_PACKAGE_JSON_HASH
ENV A_PACKAGE_JSON_HASH=${A_PACKAGE_JSON_HASH}
COPY ./package.json ./
RUN pnpm install --dangerously-allow-all-builds
RUN npx -y playwright@1.58.0 install --with-deps

ARG A_CODE_HASH
ENV A_CODE_HASH=${A_CODE_HASH}

ARG NODE_OPTIONS
ENV NODE_OPTIONS=${NODE_OPTIONS}

COPY . .

ARG A_NEW_PACKAGES_INSTALL_CMD
ENV A_NEW_PACKAGES_INSTALL_CMD=${A_NEW_PACKAGES_INSTALL_CMD}
RUN $A_NEW_PACKAGES_INSTALL_CMD

EXPOSE 4631
EXPOSE 4632
EXPOSE 9229
CMD [ "npm", "run", "start" ]
```

- Save the Dockerfile using the **Save** button in the top-right corner

This image installs:

- Required system libraries for Chromium
- Playwright browser binaries
- pnpm for dependency management

---

## Step 2: Add Playwright as a Sandbox Dependency

In addition to the Docker image, Playwright must be added as a sandbox dependency.

### Instructions

1. Open the **Sandbox Dependencies** section
2. Click the **Add (+)** button
3. Search for **playwright**
4. Select the **latest available stable version**
5. Click **Add** to confirm

After this step, Playwright is available for use within sandbox code.

---

## Usage Example

The following example demonstrates how to:

- Launch Chromium in sandbox-safe mode
- Navigate to a public website
- Generate a PDF file
- Return the file as a downloadable response

```ts
import * as T from 'types';
const fs = require('fs');
const path = require('path');
const { chromium } = require('playwright');

async function main(g: T.IAMGlobal) {
    try {
        const outDir = path.join(__dirname, 'uploads');
        if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });

        const browser = await chromium.launch({
            args: ['--no-sandbox', '--disable-setuid-sandbox'],
            headless: true,
        });
        const page = await browser.newPage();

        console.log('Navigating to https://playwright.dev ...');
        await page.goto('https://playwright.dev', { waitUntil: 'networkidle' });

        const filename = path.join(outDir, 'playwright-dev.pdf');
        console.log('Generating PDF to', filename);
        await page.pdf({ path: filename, format: 'A4' });

        await browser.close();
        console.log('PDF generated:', filename);

        return {
            __am__downloadFilePath: path.basename(filename),
            __am__downloadFolderFileName: path.basename(filename)
        }
    } catch (err) {
        console.error('Error generating PDF:', err);
    }
};
module.exports = main;
```

---

## Notes and Best Practices

- Always launch Chromium with `--no-sandbox` flags inside sandbox environments
- Ensure sufficient disk space for Playwright browser binaries
- Avoid running multiple concurrent browser instances unless memory is sufficient

---

## Summary

By overriding the sandbox Docker image and installing Playwright as a dependency, API Maker can reliably execute Playwright-based automation tasks. This setup enables advanced browser workflows while maintaining sandbox isolation and security.
