Installation & Setup
Need to generate JWTs yourself? See Authentication & security → Manual JWT generation.
Creating Repositories
| Parameter | Type | Description |
|---|---|---|
id | Optional | Repository ID. If not provided, a UUID will be auto-generated. Supports namespacing with / (e.g., team/project-alpha). |
defaultBranch (TypeScript)default_branch (Python) | Optional | Default branch name for the repository. Defaults to main. |
baseRepo (TypeScript)base_repo (Python) | Optional | GitHub repository configuration for sync. Contains owner, name, and optionally default_branch. The provider is automatically set to github. |
ttl | Optional | Token TTL in seconds for this invocation. Defaults to 3600 (1 hour). |
| Property | Type | Description |
|---|---|---|
owner | Required | GitHub repository owner (username or organization). |
name | Required | GitHub repository name. |
defaultBranch (TypeScript)default_branch (Python) | Optional | GitHub repository’s default branch. |
When baseRepo is provided, Code Storage links the repository to GitHub for automatic syncing. See the GitHub Sync guide for details.
Finding Repositories
Generating Remote URLs
The SDK automatically creates secure Git URLs with embedded JWT authentication:Error Handling (SDK)
The SDK surfaces API failures asApiError and ref update failures as RefUpdateError.
Repository Methods
Once you have a repository instance fromcreateRepo() or findOne(), you can use these methods:
listFiles()
List all files in the repository at a specific ref:listBranches()
List all branches in the repository with pagination support:listCommits()
List commit history with optional branch filtering:getBranchDiff()
Get the diff between a branch and its base:getCommitDiff()
Get the diff for a specific commit:| Parameter | Type | Description |
|---|---|---|
sha | Required | The commit SHA to get the diff for |
baseSha (TypeScript)base_sha (Python) | Optional | Base commit SHA to compare against. If not specified, compares against the commit’s parent(s) |
createCommit()
Build and push a commit to a branch using the fluent commit builder.expectedHeadSha),
repo.createCommit().send() throws a RefUpdateError containing the status, reason, and ref
details.
Builder Methods
| Method | Description |
|---|---|
addFile(path, source, options) | Attach bytes, async iterables, readable streams, or buffers. |
addFileFromString(path, contents, options) | Add UTF-8 text files. |
deletePath(path) | Remove files or folders. |
send() | Finalize the commit and receive metadata about the new commit. |
| Parameter | Type | Description |
|---|---|---|
targetBranch | Required | Branch name that will receive the commit (for example main). |
commitMessage | Required | The commit message. |
author | Required | Provide name and email for the commit author. |
expectedHeadSha | Optional | Commit SHA that must match the remote tip; omit to fast-forward unconditionally. |
baseBranch | Optional | Mirrors the base_branch metadata field. Point to an existing branch whose tip should seed targetBranch if it does not exist. When bootstrapping a new branch, omit expectedHeadSha so the service copies from baseBranch; if both fields are provided and the branch already exists, the expectedHeadSha guard still applies. |
ephemeral | Optional | Store the branch under the refs/namespaces/ephemeral/... namespace. When enabled, the commit is kept out of the primary Git remotes (for example, GitHub) but remains available through storage APIs. |
ephemeralBase | Optional | Use alongside baseBranch when the seed branch also lives in the ephemeral namespace. Requires baseBranch to be set. |
committer | Optional | Provide name and email. If omitted, the author identity is reused. |
signal | Optional | Abort an in-flight upload with AbortController. |
targetRef | Deprecated, Optional | Fully qualified ref (for example refs/heads/main). Prefer targetBranch. |
Files are chunked to 4 MiB segments under the hood, so you can stream large assets without buffering them entirely in memory. File paths are normalized relative to the repository root. ThetargetBranchmust already exist on the remote repository unless you providebaseBranch(or the repository has no refs). To initialize an empty repository, point to its default branch and omitexpectedHeadSha. To seed a missing branch inside an existing repo, setbaseBranchto the branch you want to copy and omitexpectedHeadShaso the service clones that tip before applying your changes.
Streaming Large Files
Use streams or async generators to upload large files without loading them into memory:createCommitFromDiff()
Apply a pre-generated Git patch without constructing a builder. This helper wraps the/api/v1/repos/diff-commit endpoint and is designed for workflows that already have
git diff --binary output available.
createCommitFromDiff shares the same branch metadata (expectedHeadSha, baseBranch,
ephemeral, ephemeralBase) as createCommit. Instead of calling .addFile(), pass the patch
contents through the diff field. The SDK accepts strings, Uint8Array, ArrayBuffer, Blob/File
objects, or any iterable/async iterable of byte chunks and handles chunking + base64 encoding.
The gateway applies the patch with git apply --cached --binary, so make sure your diff is
compatible with that command. It must include file headers (diff --git), mode lines, and hunk
headers. Empty patches and patches that do not apply cleanly result in a RefUpdateError with the
mapped status ( conflict, precondition_failed, etc.) and partial ref update information.
restoreCommit()
Create a commit rolling a branch back to a certain commit.| Parameter | Type | Description |
|---|---|---|
targetBranch | Required | Branch name (without refs/heads/) that receives the reset commit. |
targetCommitSha | Required | Commit SHA to reset the branch back to. Commits after this point are undone. |
author | Required | Provide name and email for the commit author. |
expectedHeadSha | Optional | Commit SHA that must match the current tip. Use to avoid races. |
commitMessage | Optional | Custom message. Defaults to Reset <branch> to "<target subject>". |
committer | Optional | Provide name and email. If omitted, the author identity is reused. |
RefUpdateError when the backend rejects the reset (for example, the branch tip
changed). On success it returns the commit metadata and the ref update; refUpdate.oldSha is the
previous branch tip (000000... when the ref did not exist) and refUpdate.newSha is the reset
commit.
createBranch()
Create a new branch from an existing branch, optionally using the ephemeral namespace:baseIsEphemeral/targetIsEphemeral to work with ephemeral refs. The method returns the API
message plus the resolved branch metadata so you can confirm creation before pushing commits.
Options
| Parameter | Type | Description |
|---|---|---|
baseBranch | Required | Source branch to copy from. Combine with baseIsEphemeral when promoting from the ephemeral namespace. |
targetBranch | Required | Destination branch name. May match baseBranch when moving between namespaces. |
baseIsEphemeral | Optional | true when the source branch lives in the ephemeral namespace (defaults to false). |
targetIsEphemeral | Optional | true to create/update an ephemeral branch instead of the default namespace. |
force | Optional | true to overwrite non–fast-forward updates. Use sparingly—most workflows should rely on expectedHeadSha. |
See the Ephemeral Branches guide for concrete promotion examples using these flags.
getFileStream()
Retrieve file content as a streaming response (standard Fetch Response):pullUpstream()
For repos that have been synced with GitHub, you can force a pull with this command. It will throw an error in any situation that doesn’t indicate a successful pull.Note this method is necessary when opting to manage your own GitHub webhook.
Error Handling (SDK)
The SDK surfaces API failures asApiError and ref update failures as RefUpdateError.