> ## Documentation Index
> Fetch the complete documentation index at: https://docs.edgespark.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# EdgeSpark SDK reference

> Reference the EdgeSpark server runtime imports and the @edgespark/web browser SDK used by the current full-stack scaffold.

EdgeSpark exposes two public SDK surfaces:

* the server runtime imports from `edgespark` and `edgespark/http`
* the browser SDK from `@edgespark/web`

This page starts with the server runtime imports, then links to the browser SDK reference.

## Main runtime imports

```typescript server/src/index.ts theme={null}
import { ctx, db, secret, storage, vars } from "edgespark";
import { auth } from "edgespark/http";
```

These imports are typed from generated declarations in `server/src/__generated__/`.

## Import contract

The public server runtime import contract is:

```typescript theme={null}
import { ctx, db, secret, storage, vars } from "edgespark";
import { auth } from "edgespark/http";
```

That is the contract AI coding agents should rely on in app code. Do not invent your own runtime client factory for server handlers.

## Example server route

```typescript server/src/index.ts theme={null}
import { db, secret } from "edgespark";
import { auth } from "edgespark/http";
import { eq } from "drizzle-orm";
import { Hono } from "hono";
import { posts } from "@defs";

const app = new Hono().get("/api/posts/:id", async (c) => {
  const stripeKey = secret.get("STRIPE_SECRET_KEY");
  const [post] = await db.select().from(posts).where(eq(posts.id, Number(c.req.param("id"))));

  return c.json({
    post,
    userEmail: auth.user.email,
    hasStripeKey: Boolean(stripeKey),
  });
});

export default app;
```

## Modules

<Columns cols={2}>
  <Card title="web" icon="monitor-smartphone" href="/sdk/web">
    The browser SDK for auth, managed auth UI, and same-origin API calls.
  </Card>

  <Card title="db" icon="database" href="/sdk/database">
    Query your D1 database with Drizzle ORM.
  </Card>

  <Card title="auth" icon="lock" href="/sdk/auth">
    Access the current user and session.
  </Card>

  <Card title="storage" icon="hard-drive" href="/sdk/storage">
    Upload, download, and manage files in R2.
  </Card>

  <Card title="secret" icon="key" href="/sdk/secrets">
    Read encrypted secrets by name.
  </Card>

  <Card title="vars" icon="sliders-horizontal" href="/sdk/vars">
    Read plain runtime variables by key.
  </Card>
</Columns>

## `ctx`

Use `ctx` for request-scoped helpers:

```typescript server/src/index.ts theme={null}
import { ctx } from "edgespark";
import { Hono } from "hono";

async function sendToAnalytics(event: unknown) {
  await fetch("https://example.com/analytics", {
    method: "POST",
    body: JSON.stringify(event),
  });
}

const app = new Hono().post("/api/events", async (c) => {
  const event = await c.req.json();

  ctx.runInBackground(sendToAnalytics(event));

  return c.json({
    received: true,
    environment: ctx.environment,
  });
});

export default app;
```

## Generated types

Run `edgespark pull types` to refresh the generated declarations for `edgespark` imports. Run `edgespark pull types --check` to detect stale types in CI or before deploy.

## See also

<Columns cols={2}>
  <Card title="The client object" icon="book" href="/concepts/client-object">
    Conceptual overview of the runtime SDK and how it is injected per request.
  </Card>

  <Card title="Project structure" icon="folder" href="/guides/project-structure">
    Where generated files and repo-authored defs live in the scaffold.
  </Card>
</Columns>
