@autoschema/sdk
v0.1.2
Published
AutoSchema SDK for React and Next.js
Readme
@autoschema/sdk
AutoSchema SDK for React and Next.js projects.
Install
npm i @autoschema/sdkReact-only projects can use this package without Next.js. The next peer dependency is optional and only needed when you import @autoschema/sdk/next.
Environment variables (recommended naming)
Use env vars for API keys instead of hard-coding them in source control.
Next.js (App Router / next)
Create .env.local in the project root (do not commit secrets):
NEXT_PUBLIC_AUTOSCHEMA_KEY=sk_...
NEXT_PUBLIC_AUTOSCHEMA_MODE=cdn_hybrid
# Optional — defaults match production AutoSchema if omitted:
# NEXT_PUBLIC_AUTOSCHEMA_API_BASE=https://www.autoschema.app
# NEXT_PUBLIC_AUTOSCHEMA_STATIC_BASE=https://cdn.autoschema.app/schemasNEXT_PUBLIC_AUTOSCHEMA_MODE is optional; if unset, pass mode explicitly in code. Valid values align with the SDK: cdn_hybrid, cdn_static_only, api_dynamic.
Vite / React SPA
Vite exposes only variables prefixed with VITE_:
VITE_AUTOSCHEMA_KEY=sk_...
VITE_AUTOSCHEMA_MODE=cdn_hybrid
# Optional:
# VITE_AUTOSCHEMA_API_BASE=https://www.autoschema.app
# VITE_AUTOSCHEMA_STATIC_BASE=https://cdn.autoschema.app/schemasCreate React App (react-scripts)
Use the REACT_APP_ prefix:
REACT_APP_AUTOSCHEMA_KEY=sk_...
REACT_APP_AUTOSCHEMA_MODE=cdn_hybridQuick Start (React)
import { AutoSchemaProvider, AutoSchemaHead } from "@autoschema/sdk/react";
export function App() {
return (
<AutoSchemaProvider
apiKey={import.meta.env.VITE_AUTOSCHEMA_KEY!}
apiBase="https://www.autoschema.app"
staticBase="https://cdn.autoschema.app/schemas"
mode={(import.meta.env.VITE_AUTOSCHEMA_MODE as "cdn_hybrid") ?? "cdn_hybrid"}
>
<AutoSchemaHead />
{/* app */}
</AutoSchemaProvider>
);
}(With CRA, use process.env.REACT_APP_AUTOSCHEMA_KEY instead of import.meta.env.)
Quick Start (Next.js App Router)
import { AutoSchemaScript } from "@autoschema/sdk/next";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html>
<head>
<AutoSchemaScript
apiKey={process.env.NEXT_PUBLIC_AUTOSCHEMA_KEY!}
mode={
(process.env.NEXT_PUBLIC_AUTOSCHEMA_MODE as
| "cdn_hybrid"
| "cdn_static_only"
| "api_dynamic") ?? "cdn_hybrid"
}
/>
</head>
<body>{children}</body>
</html>
);
}Server helper for Next.js
import { getAutoSchemaForNext } from "@autoschema/sdk/next";
const schema = await getAutoSchemaForNext({
apiKey: process.env.AUTOSCHEMA_KEY!,
url: "/about",
mode: "cdn_hybrid",
});Modes
cdn_hybrid: static CDN -> edge static -> dynamic API (fallbacks only on cache/static miss).cdn_static_only: static CDN only (no API fallback).api_dynamic: direct/api/schema.
Publish (maintainers)
From this package directory (not the monorepo root):
cd packages/autoschema-sdk
npm version patch # or: manually set "version" in package.json
npm run build
npm publish --access publicprepublishOnly runs npm run build automatically before publish.
