Guides
Using with AI Agents
Integrate Coregit into your AI coding agent workflow — from repo creation to commit and clone.
Why Coregit for AI Agents?
Traditional Git requires a filesystem, a git binary, and SSH keys. AI agents running in serverless environments don't have these. Coregit replaces all of that with a REST API:
- No containers, no git binary, no cold starts
- Multi-file commits in a single HTTP call
- Instant rollback via snapshots
- Standard
git clonefor the final result
Full Workflow
1. Create a Repository
const res = await fetch('https://api.coregit.dev/v1/repos', {
method: 'POST',
headers: {
'x-api-key': process.env.COREGIT_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({
slug: 'agent-output',
visibility: 'private',
}),
});
const repo = await res.json();
// { id: "repo_abc", slug: "agent-output", ... }2. Generate Code and Commit
Your agent generates files, then commits them all at once:
const commitRes = await fetch(
'https://api.coregit.dev/v1/repos/agent-output/commits',
{
method: 'POST',
headers: {
'x-api-key': process.env.COREGIT_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({
branch: 'main',
message: 'feat: implement user authentication',
changes: [
{
path: 'src/auth.ts',
content: `import { hash, verify } from './crypto';
export async function authenticate(email: string, password: string) {
const user = await db.users.findByEmail(email);
if (!user) throw new Error('User not found');
const valid = await verify(password, user.passwordHash);
if (!valid) throw new Error('Invalid password');
return { id: user.id, email: user.email };
}`,
},
{
path: 'src/middleware.ts',
content: `import { authenticate } from './auth';
export async function authMiddleware(req, res, next) {
const token = req.headers.authorization?.split(' ')[1];
if (!token) return res.status(401).json({ error: 'No token' });
try {
req.user = await verifyToken(token);
next();
} catch {
res.status(401).json({ error: 'Invalid token' });
}
}`,
},
{
path: 'src/index.ts',
content: `import { authMiddleware } from './middleware';
app.use('/api', authMiddleware);
app.listen(3000);`,
},
],
}),
}
);
const commit = await commitRes.json();
// { sha: "7f3b...", tree_sha: "a1c2...", parent_sha: "0000..." }3. Create a Snapshot Before Risky Changes
await fetch('https://api.coregit.dev/v1/repos/agent-output/snapshots', {
method: 'POST',
headers: {
'x-api-key': process.env.COREGIT_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: 'before-refactor',
branch: 'main',
}),
});4. Roll Back if Something Breaks
await fetch(
'https://api.coregit.dev/v1/repos/agent-output/snapshots/before-refactor/restore',
{
method: 'POST',
headers: {
'x-api-key': process.env.COREGIT_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({ target_branch: 'main' }),
}
);5. Clone the Result
The user (or CI) clones with standard Git:
git clone https://myorg:cgk_live_KEY@api.coregit.dev/myorg/agent-output.gitConflict Detection
When multiple agents work on the same repo, use parent_sha to detect conflicts:
// Get current branch head
const branchRes = await fetch(
'https://api.coregit.dev/v1/repos/agent-output/branches/main',
{ headers: { 'x-api-key': process.env.COREGIT_API_KEY } }
);
const { sha: currentHead } = await branchRes.json();
// Commit with CAS check
const commitRes = await fetch(
'https://api.coregit.dev/v1/repos/agent-output/commits',
{
method: 'POST',
headers: {
'x-api-key': process.env.COREGIT_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({
branch: 'main',
message: 'update config',
parent_sha: currentHead, // Will return 409 if branch moved
changes: [
{ path: 'config.json', content: '{"v": 2}' },
],
}),
}
);
if (commitRes.status === 409) {
// Branch was updated by another agent — re-read and retry
}Helper Function
Wrap the API in a reusable function:
class CoregitClient {
constructor(private apiKey: string, private baseUrl = 'https://api.coregit.dev/v1') {}
private async request(path: string, options?: RequestInit) {
const res = await fetch(`${this.baseUrl}${path}`, {
...options,
headers: {
'x-api-key': this.apiKey,
'Content-Type': 'application/json',
...options?.headers,
},
});
if (!res.ok) throw new Error(`Coregit API error: ${res.status}`);
return res.json();
}
createRepo(slug: string, visibility: 'private' | 'public' = 'private') {
return this.request('/repos', {
method: 'POST',
body: JSON.stringify({ slug, visibility }),
});
}
commit(repoSlug: string, branch: string, message: string, actions: any[]) {
return this.request(`/repos/${repoSlug}/commits`, {
method: 'POST',
body: JSON.stringify({ branch, message, actions }),
});
}
snapshot(repoSlug: string, name: string, branch: string) {
return this.request(`/repos/${repoSlug}/snapshots`, {
method: 'POST',
body: JSON.stringify({ name, branch }),
});
}
restore(repoSlug: string, snapshotName: string, targetBranch: string) {
return this.request(`/repos/${repoSlug}/snapshots/${snapshotName}/restore`, {
method: 'POST',
body: JSON.stringify({ target_branch: targetBranch }),
});
}
readFile(repoSlug: string, ref: string, path: string) {
return this.request(`/repos/${repoSlug}/blob/${ref}/${path}`);
}
}
// Usage
const git = new CoregitClient(process.env.COREGIT_API_KEY!);
await git.createRepo('my-project');
await git.commit('my-project', 'main', 'init', [
{ path: 'README.md', content: '# Hello' },
]);