@openpolicy/sdk
v0.0.34
Published
Public API for defining privacy policies with OpenPolicy
Readme
@openpolicy/sdk
TypeScript SDK for defining privacy and cookie policies as code.
Part of OpenPolicy — a policy-as-code framework that compiles legal agreements from TypeScript at build time.
Not legal advice. OpenPolicy generates policy documents from your config. It does not provide legal advice. Have a lawyer review your policies before publication. See the legal notice.
Install
bun add @openpolicy/sdk
# or: npm install @openpolicy/sdkUsage
Privacy policy
// openpolicy.ts
import { ContractPrerequisite, defineConfig, LegalBases, Voluntary } from "@openpolicy/sdk";
export default defineConfig({
company: {
name: "Acme",
legalName: "Acme, Inc.",
address: "123 Main St, San Francisco, CA 94105",
contact: { email: "[email protected]" },
},
effectiveDate: "2026-01-01",
jurisdictions: ["eu", "us-ca"],
data: {
collected: {
"Account information": ["Email address", "Display name"],
"Usage data": ["Pages visited", "Session duration"],
},
context: {
"Account information": {
purpose: "To create and manage user accounts",
lawfulBasis: LegalBases.Contract,
retention: "Until account deletion",
provision: ContractPrerequisite("We cannot create or operate your account."),
},
"Usage data": {
purpose: "To understand product usage and improve the service",
lawfulBasis: LegalBases.LegitimateInterests,
retention: "13 months",
provision: Voluntary("None — your service is unaffected."),
},
},
},
thirdParties: [
{ name: "Vercel", purpose: "Hosting" },
{ name: "Plausible", purpose: "Privacy-friendly analytics" },
],
});Cookie policy
// openpolicy.ts
import { defineConfig, LegalBases } from "@openpolicy/sdk";
export default defineConfig({
company: {
name: "Acme",
legalName: "Acme, Inc.",
address: "123 Main St, San Francisco, CA 94105",
contact: { email: "[email protected]" },
},
effectiveDate: "2026-01-01",
jurisdictions: ["eu", "us-ca"],
cookies: {
used: { essential: true, analytics: true, marketing: false },
context: {
essential: { lawfulBasis: LegalBases.LegalObligation },
analytics: { lawfulBasis: LegalBases.Consent },
marketing: { lawfulBasis: LegalBases.Consent },
},
},
});Rendering policies
The SDK exports types and helper functions for defining policies. To render them in your app, pair it with one of:
@openpolicy/react—<PrivacyPolicy />/<CookiePolicy />components for React@openpolicy/vue— Vue 3 components@openpolicy/vite— Vite plugin that scans source forcollecting()/thirdParty()annotations at build time@openpolicy/cli— one-command project setup: installs the right packages, scaffolds a config, and prints a prompt for your coding agent
Documentation
Full field reference and guides: openpolicy.sh/docs
AI Agents
If you use an AI coding agent (Claude Code, Cursor, Copilot, etc.), run:
npx @tanstack/intent@latest installThis installs skill files that give your agent accurate, up-to-date guidance for OpenPolicy APIs.
