# PolicyGrain Complete Documentation Corpus Source: https://docs.policygrain.com OpenAPI 3.1.0: https://docs.policygrain.com/openapi.json Skill: https://docs.policygrain.com/SKILL.md --- # What PolicyGrain does PolicyGrain generates ten cited DPDP Act, IT (SPDI), Consumer Protection Act, and CERT-In documents from plain answers, serves a 4.94 KB gzipped equal-choice consent banner, verifies installs in headless Chromium, and seals consent receipts in SHA-256 hash chains with daily Ed25519 Merkle roots. ## Platform modules 1. Cited Notices (modules/legal-corpus & modules/documents): 10 document kinds assembled deterministically from pinned legal-sources with inline statutory citations (s.5(1), r.3), in English today; Hindi and the other Eighth Schedule languages are rolling out next. 2. Hosted Pages (modules/hosted-docs): publish approved versions to pages.policygrain.com with zero JavaScript and zero cookies. 3. Consent Banner v2 (modules/sites & browser/banner): 4.94 KB gzipped loader (12.70 KB total), DPDP s.6(4) equal-choice Accept/Reject, all 7 Google Consent Mode v2 signals, synchronous returning-visitor restore, native Global Privacy Control, and Shadow DOM click-to-load iframe blocking. 4. Evidence Ledger (modules/evidence & modules/usage): HMAC-SHA256 IP hashing, insert-only DB role, per-site SHA-256 hash chain, daily Ed25519 Merkle roots (evidence.evidence_roots), limit-aware JSON/CSV export with formula neutralization, and a 10,000 monthly free-tier counter that never blocks ingest. 5. CheckMyDPDP Scanner & Verification (modules/scanner & modules/verify): isolated 3-pass headless Chromium checks across 11 evidence-backed observations, plus a 100% local Chrome MV3 Inspector extension. 6. Ecosystem Plugins & Coding Agents: GTM Community Template, WordPress Plugin, Shopify Theme App Extension, @policygrain/react & Next.js SDK, @policygrain/mcp (16 tools), @policygrain/cli, and SKILL.md v0.9. ## Reading these docs with an agent - Fetch /llms.txt for the structured index, or /llms-full.txt for the complete concatenated Markdown corpus. - Append .md to any documentation path (or /index.md for this page) for clean Markdown. - Fetch /openapi.json (or https://api.policygrain.com/v1/openapi.json) for the merged OpenAPI 3.1.0 contract. - Fetch /SKILL.md for the v0.9 coding-agent operational playbook. --- # 5-minute DPDP quickstart Mint a scoped API key, create a profile, publish a cited privacy notice, install the 4.94 KB gzipped banner loader, and export a verifiable receipt pack. ## 1. Mint a scoped API key Call POST /v1/keys with the website owner's email and the narrowest scopes needed. Store the returned pgk_ secret in POLICYGRAIN_API_KEY and never commit it to git. #### cURL ```bash curl -X POST https://api.policygrain.com/v1/keys \ -H "Content-Type: application/json" \ -d '{"email": "owner@example.in", "scopes": ["documents", "sites:write"]}' ``` #### CLI ```bash npx policygrain init --domain example.in npx checkmydpdp https://example.in --json ``` #### TypeScript SDK ```ts import { createClient, httpTransport } from "@policygrain/sdk"; const client = createClient({ transport: httpTransport({ baseUrl: "https://api.policygrain.com", tokens: { token: () => process.env.POLICYGRAIN_API_KEY ?? null }, }), }); const account = await client.account(); ``` #### MCP ```json { "mcpServers": { "policygrain": { "command": "npx", "args": ["-y", "@policygrain/mcp"], "env": { "POLICYGRAIN_API_KEY": "${POLICYGRAIN_API_KEY}" } } } } ``` ## 2. Create a profile and generate cited notices Read GET /v1/questions to see the pinned Tier 1 question set, create a profile with POST /v1/profiles, trigger POST /v1/generations, and publish the approved version to pages.policygrain.com. ```bash curl -X POST https://api.policygrain.com/v1/generations \ -H "Authorization: Bearer $POLICYGRAIN_API_KEY" \ -H "Idempotency-Key: gen-001" \ -H "Content-Type: application/json" \ -d '{"profile_id": "prof_01..."}' ``` ## 3. Verify your domain and embed Consent Banner v2 Register your site with POST /v1/sites, verify domain ownership via DNS TXT or HTTPS well-known file at POST /v1/sites/{id}/domains/{domain}/verify, publish your banner config, and drop the single script tag into your root layout. #### HTML ```html ``` #### React / Next.js ```tsx import { PolicyGrainProvider, PolicyGrainScript } from "@policygrain/react/next"; export default function RootLayout({ children }: { children: React.ReactNode }) { return ( {children} ); } ``` #### WordPress / GTM ```bash # WordPress: Activate PolicyGrain DPDP plugin, enter Site Key in Settings > PolicyGrain # Shortcode for hosted notice embed: [policygrain_notice slug="example-privacy"] ``` ## 4. Verify in Chromium and export signed receipts Run POST /v1/sites/{id}/verifications to confirm the banner loads before optional scripts fire, then inspect daily Ed25519 Merkle roots at GET /v1/sites/{id}/seal and export a date-bounded JSON/CSV evidence pack. --- # Concepts Six architectural invariants govern every API route, browser script, and generated notice in PolicyGrain. ## API keys, scopes and account claiming Every authenticated route accepts Authorization: Bearer with either a pgk_ API key or a 15-minute pgs_ web session token. Four scopes gate access: documents, sites:write, evidence:read (requires a verified account), and keys. When a coding agent creates an unverified account via POST /v1/keys and the human owner later signs in via WorkOS, every pre-claim key becomes suspended (403 key_suspended) until the owner explicitly keeps or revokes it on /keys. ## Questions, tiers and visibility Tier 1 questions capture the core data-processing facts shown in the web app. Tier 2 questions refine optional clauses through the API and carry shows_if conditions evaluated deterministically against earlier answers. ## Cited documents, provenance and material_version Documents are assembled deterministically from pinned policygrain/legal-sources templates—never hallucinated per customer. Identical answer digests reuse content-addressed versions, and every clause retains its statutory register citation (such as DPDP Act s.5(1) or DPDP Rules r.3). > **Deterministic assembly (ADR 0014)**: PolicyGrain is not a law firm and does not give legal advice. Every generated notice ends with its pinned corpus provenance and requires explicit owner confirmation before publication. ## Owner answers vs agent-inferred answers Answers submitted by coding agents are tagged source: 'agent_inferred' and hold generated documents in draft state until the human account owner confirms them via PATCH /v1/profiles/{id} with confirm. ## Sites, verified domains and versioned banner configs Each site verifies domain ownership via DNS TXT or HTTPS well-known challenge before live browser verification runs. Banner configurations are immutable numbered versions published to the CDN; incrementing material_version prompts returning visitors to re-confirm. ## Consent receipts, trust classes and Merkle seals Visitor choices arrive at POST /v1/receipts, are classified by origin and config match (origin_ok, origin_unexpected, config_mismatch, rate_flagged), written by an insert-only PostgreSQL role into a per-site SHA-256 chain, and sealed daily with Ed25519 Merkle roots. --- # Generate cited notices (10 document kinds) PolicyGrain renders ten deterministic documents mapped to the DPDP Act 2023, DPDP Rules 2025, IT (SPDI) Rules 2011, Consumer Protection Act, and CERT-In directions. ## All 10 supported document kinds Document types generated by modules/documents and modules/legal-corpus | doc_type | Document | Primary statutory anchor | | --- | --- | --- | | privacy-notice | Privacy Notice | DPDP Act s.5(1), DPDP Rules r.3, SPDI r.4(1) | | re-notice | Notice to Existing Users | DPDP Act s.5(2) | | consent-notice | Consent Notice | DPDP Act s.5(1) & s.6(1) | | short-notice | Just-in-Time Short Notice | DPDP Act s.5(1), SPDI r.5(3) | | rights-grievance | Data Principal Rights & Grievance Page | DPDP Act ss.11–14, DPDP Rules r.9 & r.14 | | cookie | Cookie & Tracker Notice | DPDP Act s.6(1) & s.6(4) | | dpa | Data Processing Agreement | DPDP Act s.8(2) | | terms | Terms of Service | IT (Intermediary Guidelines) Rules r.3(1) | | aup | Acceptable Use Policy | IT Rules r.3(1)(b) | | disclaimer | Legal & Platform Disclaimer | Consumer Protection (E-Commerce) Rules & CERT-In | ## Create a profile and submit answers #### cURL ```bash curl -X POST https://api.policygrain.com/v1/profiles \ -H "Authorization: Bearer $POLICYGRAIN_API_KEY" \ -H "Idempotency-Key: prof-init-1" \ -H "Content-Type: application/json" \ -d '{ "answers": { "M-02": ["saas", "ai"], "M-04": "yes", "M-07": ["service_delivery", "account", "billing", "analytics"] }, "fields": { "org.legal_name": "Example Private Limited", "org.website": "https://example.in", "officer.name": "Asha Menon", "officer.email": "grievance@example.in", "contact.privacy": "privacy@example.in" } }' ``` #### TypeScript SDK ```ts const profile = await client.createProfile({ answers: { "M-02": ["saas", "ai"], "M-04": "yes", "M-07": ["service_delivery", "account", "billing", "analytics"], }, fields: { "org.legal_name": "Example Private Limited", "org.website": "https://example.in", "officer.name": "Asha Menon", "officer.email": "grievance@example.in", "contact.privacy": "privacy@example.in", }, }); ``` ## Start generation, check provenance and download Call POST /v1/generations with your profile_id and poll GET /v1/generations/{id} until status is done. Fetch GET /v1/documents/{id}/versions/latest for JSON + full statutory provenance, or append ?format=html or ?format=md for standalone outputs. Answer keys are question ids from GET /v1/questions (M-02, M-04, M-07, ...); field keys are the dotted legal-sources field ids (org.legal_name, officer.email, contact.privacy, ...). An answer can also be sent as {"value": ..., "source": "agent_inferred"}; the owner then confirms it before the document can be final. > **Languages**: Generated documents are in English today. DPDP Act s.5(3) lets a Data Principal ask for a notice in English or any language in the Eighth Schedule to the Constitution; Hindi and the other Eighth Schedule languages are rolling out next. Each clause version records its pinned legal-sources commit hash. --- # Refine clauses with Tier 2 questions (API) Tier 2 questions refine sector-specific processing, retention schedules, cross-border transfers, and children's data handling beyond the Tier 1 set. ## When to use Tier 2 questions Read GET /v1/questions and filter for tier: 2. Each question includes shows_if conditions and an affects array listing exactly which document types change when you answer it. ## Patch answers and confirm inferred values ```bash curl -X PATCH https://api.policygrain.com/v1/profiles/prof_01... \ -H "Authorization: Bearer $POLICYGRAIN_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "answers": { "T2-04": "90_days" }, "confirm": ["M-01", "M-02"] }' ``` ## Regenerate with content-addressed versions Call POST /v1/generations after patching. Only documents whose inputs changed receive a new version number; unchanged documents preserve their prior version and material_version. --- # Publish hosted document pages Hosted pages on pages.policygrain.com render pure semantic HTML with zero scripts and zero cookies, ensuring privacy notices never themselves track visitors. ## Requirements before publishing - The target document version must be in final state (all agent_inferred answers confirmed by the owner). - The caller holds the documents scope or a signed-in web session. ## Publish via PUT /v1/documents/{id}/publication ```bash curl -X PUT https://api.policygrain.com/v1/documents/doc_01.../publication \ -H "Authorization: Bearer $POLICYGRAIN_API_KEY" \ -H "Content-Type: application/json" \ -d '{"slug": "example-privacy"}' ``` PUT publishes the newest final version. The body takes only slug, and only the first time: the slug then stays this document's, so later calls send {} (or the same slug) and a different slug is refused. The call is idempotent; it answers 200 once the page is live and 202 while the edge is still syncing. Link the returned https://pages.policygrain.com/{slug} URL from your website footer and banner notice link. Previous published versions remain readable at /{slug}/versions/{n}. ## Unpublishing and material_version bumps Call DELETE /v1/documents/{id}/publication to unpublish and purge the hosted page from the edge. When a published privacy or consent notice changes materially, bump your banner config material_version so returning visitors see the updated notice. --- # Consent Banner v2 (4.94 KB gzipped) Consent Banner v2 ships a 4.94 KB gzipped loader (12.70 KB total), enforces DPDP s.6(4) equal-choice Accept/Reject prominence, bridges all 7 Google Consent Mode v2 signals, honours Global Privacy Control, and blocks third-party embeds until consent. ## What Consent Banner v2 enforces by default Shipped browser/banner v2 technical specifications | Capability | Implementation | | --- | --- | | Bundle budget | 4.94 KB gzipped loader; 12.70 KB total with renderer | | DPDP s.6(4) parity | Equal-weight Accept and Reject controls on layer 1; zero pre-ticked optional toggles; zero scroll lock | | Google Consent Mode v2 | All 7 signals (ad_storage, analytics_storage, ad_user_data, ad_personalization, functionality_storage, personalization_storage, security_storage) | | Returning visitors | Synchronous cookie/localStorage restore before tag initialization; expired or older material_version re-prompts cleanly | | Global Privacy Control | Reads navigator.globalPrivacyControl; respects opt-out signals automatically | | Iframe blocking | Shadow DOM click-to-load placeholders for YouTube, Google Maps, and Vimeo embeds until visitor opts in | ## 1. Register site, verify domain and publish config ```bash # 1. Verify domain ownership (DNS TXT or /.well-known/policygrain-verify.txt) curl -X POST https://api.policygrain.com/v1/sites/site_01.../domains/example.in/verify \ -H "Authorization: Bearer $POLICYGRAIN_API_KEY" # 2. Publish banner config version 1 to cdn.policygrain.com # (publish takes a numeric version; read GET /v1/sites/{id}/configs/latest to find the newest n) curl -X POST https://api.policygrain.com/v1/sites/site_01.../configs/1/publish \ -H "Authorization: Bearer $POLICYGRAIN_API_KEY" \ -H "Idempotency-Key: pub-banner-1" ``` ## 2. Add the script tag and gate optional scripts or iframes #### HTML Snippet ```html ``` #### React / Next.js ```tsx import { PolicyGrainGate, usePolicyGrainConsent } from "@policygrain/react"; export function MapEmbed() { const { openPreferences } = usePolicyGrainConsent(); return ( Allow analytics to load map} >