When using Vercel Sandbox with private repositories, you need to authenticate with a GitHub personal access token or Github App token. This guide explains how.
The Sandbox.create() method initializes the environment with the provided repository and configuration options, including authentication credentials, timeout, and exposed ports. Once created, you can execute commands inside the sandboxed environment using runCommand.
GitHub offers several authentication methods. Choose the one that best fits your use case.
Fine-grained tokens offer repository-specific access and enhanced security. This is the recommended approach for individual developers.
- Go to GitHub Settings → Developer settings → Personal access tokens → Fine-grained tokens
- Click Generate new token
- Configure the token:
- Token name: Give it a descriptive name (e.g., "Vercel Sandbox Access")
- Expiration: Set an appropriate expiration date
- Resource owner: Select your account or organization
- Repository access: Choose "Selected repositories" and select your private repo
- Repository permissions: Grant
Contents: ReadandMetadata: Read
- Click Generate token and copy the token
For platforms where users install your GitHub App, use installation access tokens. This is the recommended approach for multi-tenant platforms.
GitHub App tokens provide several advantages:
- Short-lived tokens (1 hour) reduce security risk
- Users grant access through GitHub's familiar OAuth flow
- Tokens are scoped to specific installations
- Higher rate limits than personal access tokens
Generate an installation token using the GitHub API:
Classic tokens work similarly to fine-grained tokens but with broader scope. Create one at GitHub Settings → Developer settings → Personal access tokens → Tokens (classic) with the repo scope.
When building a platform where users bring their own private repositories, follow this pattern:
- Create a GitHub App and configure it with the
Contents: Readpermission - Users install your app on their repositories through GitHub's OAuth flow
- Store the installation ID when the user completes the installation
- Generate a fresh token each time you create a sandbox
- Pass the token to
Sandbox.create()as shown above
After creating the sandbox, run commands to install dependencies and start your application:
- Never log or store tokens in plain text
- Use environment variables to pass tokens to your application
- Prefer short-lived tokens (GitHub App installation tokens) over long-lived personal access tokens
- Grant minimal permissions Read is sufficient for cloning
- Rotate tokens regularly if using personal access tokens