Installing the SDK
Safran is not published to a package registry. You clone the repository and import its packages straight from the checkout: every package exports its TypeScript source, and Bun runs it without a build step. This page shows the two ways of importing them that work, the one that looks obvious and does not, and a first script.
What you need
- Bun 1.3 or newer. The packages use
Bun.file,bun:sqliteandBun.spawn, so Node.js cannot run them. gitonPATH, for cloning the repositories of your code hosts.- A profile. The examples use the acme profile from the quickstart.
git clone <repository-url> /work/safran
cd /work/safran
bun installThe packages
| Package | Import it for |
|---|---|
@safran/core | createSafran and everything a caller needs: the instance, its six sub-APIs, the error helpers, the domain types, defineProfile and the define* plugin helpers. Enough for most programs. |
@safran/kit | The contracts and helpers for plugin authors: Tracker, CodeHost, DocSource, Embedder, createHttpClient, cursor and JSON helpers. @safran/core re-exports the common part of it. |
@safran/kit/testing | In-memory fakes (FakeTracker, FakeCodeHost, FakeCodeIndex, FakeEmbedder, FakeDocSource) and mockFetch. See testing. |
@safran/profile | Loading, validating and querying a profile without building an instance. See company and docs. |
@safran/mcp | The MCP server as a library. See the API reference. |
@safran/discover | Drafting groups, teams and glossary from merged pull requests. See the API reference. |
@safran/eval | Golden sets and retrieval measurement. See the API reference. |
The other packages in packages/ (@safran/trackers, @safran/hosts, @safran/docsources, @safran/embedders, @safran/codeindex) hold the built-in connectors and the code index. @safran/core loads them for you.
How the packages resolve
Two facts decide how you import Safran:
- Each package's
package.jsonpointsexportsat TypeScript source ("exports": "./src/index.ts"). There is no compiled output. - The root
package.jsondeclares"workspaces": ["packages/*"], and Bun links a workspace package only into the packages that list it as a dependency. There is no sharednode_modules/@safranat the root.
The second fact means that a loose script dropped anywhere in the checkout cannot import the packages:
$ bun scripts/hello.ts
error: Cannot find module '@safran/core' from '/work/safran/scripts/hello.ts'Your code has to live in a package that declares the dependency. Either of the next two sections does that.
Option A: a package inside the checkout
Add your own workspace package next to Safran's. This is the shortest path and needs nothing outside the checkout.
-
Create
packages/acme-scripts/package.json:{ "name": "acme-scripts", "private": true, "type": "module", "dependencies": { "@safran/core": "workspace:*", "@safran/kit": "workspace:*", "zod": "^4.0.0" } }List
@safran/kitonly if you import@safran/kitor@safran/kit/testing, andzodonly if you write plugins. -
Link it:
cd /work/safran bun installbun installrewritesbun.lock, because your package is now part of the workspace. Keep that change local. -
Write scripts in
packages/acme-scripts/and run them withbun.
Option B: your own project, linked to the checkout
To keep your code in a separate repository, register the packages with bun link and depend on them through the link: protocol.
-
Register each package you import. This writes nothing into the checkout:
cd /work/safran/packages/core && bun link cd /work/safran/packages/kit && bun link -
In your project, declare them:
{ "name": "acme-tools", "private": true, "type": "module", "dependencies": { "@safran/core": "link:@safran/core", "@safran/kit": "link:@safran/kit", "zod": "^4.0.0" }, "devDependencies": { "@types/bun": "latest", "typescript": "^5.9.3" } } -
Install:
cd /work/acme-tools bun install
node_modules/@safran/core is then a symbolic link to /work/safran/packages/core. Safran's own dependencies (@safran/kit, zod, the connectors) are found from there, so bun install must have been run in the checkout too. A link is per machine: every developer, and CI, repeats step 1.
A first script
The script builds an instance from ./safran.yaml, reads one ticket and searches the indexed code with the ticket's title.
import { createSafran } from "@safran/core";
await using safran = await createSafran();
const ticket = await safran.tickets.get("PAY-1423");
console.log(`${ticket.key} [${ticket.status}] ${ticket.title}`);
const { hits, effectiveQuery } = await safran.code.search(ticket.title, { groups: ["backend"], limit: 3 });
console.log(`searched for: ${effectiveQuery}`);
for (const hit of hits) console.log(`${hit.repo}/${hit.path}:${hit.startLine}-${hit.endLine} ${hit.score}`);Run it from the directory that holds safran.yaml, with the tokens the profile names in the environment (see secrets), after safran sync has cloned and indexed the repositories once:
$ cd /work/acme
$ bun /work/safran/packages/acme-scripts/first.ts
PAY-1423 [Done] Kısmi iade işlemi İstanbul şubesinde başarısız oluyor
searched for: Kısmi iade işlemi İstanbul şubesinde başarısız oluyor refund chargeback
ledger/src/entry.ts:1-9 0.4852
payments-api/src/refund/service.ts:1-19 0.4745
payments-api/src/refund/policy.ts:1-11 0.474The second line shows the glossary at work: the ticket says iade, the code says refund. The scores come from two small example repositories and a local bge-m3 model; yours will differ. Before anything is indexed, the search returns no hits instead of failing.
What the script used:
createSafran()with no options reads./safran.yamland keeps its data in./data.await usingcloses the instance when the scope ends. See lifecycle.tickets.getaccepts whatever a person would paste: a key or a URL.code.searchreturns ranked snippets, not whole files.
TypeScript settings
Bun runs the scripts with no configuration. A tsconfig.json matters for your editor and for tsc --noEmit, and one detail is specific to Safran: because the packages export source, your compiler typechecks Safran's files with your options. Use the options the repository itself is checked with:
{
"compilerOptions": {
"target": "ESNext",
"module": "Preserve",
"moduleResolution": "bundler",
"lib": ["ESNext"],
"types": ["bun"],
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true,
"noEmit": true
}
}| Setting | Why |
|---|---|
target and lib: ESNext | await using and Symbol.asyncDispose need the explicit resource management types. With an older lib the declaration await using safran = … does not compile. |
moduleResolution: bundler | The packages are imported by their exports map and by extension-less relative paths. |
types: ["bun"] | The sources use Bun globals. Install @types/bun. |
exactOptionalPropertyTypes | The public types are written for it: an optional field is left out, never set to undefined. The examples in this guide compile with it on. |
If your toolchain cannot use await using, call close() yourself:
import { createSafran } from "@safran/core";
const safran = await createSafran();
try {
console.log((await safran.tickets.get("PAY-1423")).title);
} finally {
await safran.close();
}Common mistakes
| Symptom | Cause |
|---|---|
Cannot find module '@safran/core' | The script is not inside a package that declares the dependency. Use option A or B. |
Cannot find package 'zod' | Your package imports zod without listing it. Add it to your own dependencies. |
PROFILE_INVALID: Profile not found | createSafran() looks for safran.yaml in the current directory. Run from the profile's directory or pass profile. |
AUTH_MISSING on the first ticket call | The environment variable named in the profile is not set in the shell that runs the script. |