Skip to content

API quickstart

This is the developer path. You’re building a custom agent (customer-service bot, internal tool, vertical AI product) and wiring in InPolicy yourself. If you’re deploying for employees, see Deploy for your team instead.

  • A tenant at app.inpolicy.ai.
  • A few published policies. If you don’t have any yet, use the Policy Inbox to ingest a policy doc first.
  • Node.js 18+.
  1. Sign in at app.inpolicy.ai as a tenant admin.
  2. Go to Settings → API Keys.
  3. Create a key named “dev”. Copy the plaintext. It’s shown only once.
Terminal window
npm install @inpolicy/sdk

Step 3 — Your first policy-aware response

Section titled “Step 3 — Your first policy-aware response”
import { InPolicyClient } from '@inpolicy/sdk';
import OpenAI from 'openai';
const ip = new InPolicyClient({ apiKey: process.env.INPOLICY_API_KEY! });
const openai = new OpenAI();
async function respond(sessionId: string, userMessage: string) {
// 1. Record the user turn and get applicable policies.
const turn = await ip.recordTurn({
sessionId,
turn: { role: 'user', content: userMessage },
});
// 2. Build the system prompt with the injection block.
const systemPrompt = [
'You are a helpful support agent for Acme Corp.',
turn.injectionBlock,
].join('\n\n');
// 3. Call your LLM.
const completion = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userMessage },
],
});
const output = completion.choices[0].message.content ?? '';
// 4. Record the assistant turn. (Omit checkOutput for the quickstart.)
await ip.recordTurn({
sessionId,
turn: { role: 'assistant', content: output },
});
return output;
}
// Usage: one session per conversation.
const sessionId = crypto.randomUUID();
console.log(await respond(sessionId, "I'm 16 — can I buy this?"));

Run it:

Terminal window
export INPOLICY_API_KEY=inp_live_...
export OPENAI_API_KEY=sk-...
tsx quickstart.ts

You should see an output where the LLM actually references the policies you’ve authored. If you have a COPPA-related policy, for instance, the response will apply it.

recordTurn returns more than injectionBlock. The diff field tells you what changed this turn:

if (turn.diff.newlyApplicable.length > 0) {
console.log('[InPolicy]', 'newly active this turn:',
turn.diff.newlyApplicable.map(p => `${p.citationRef} (${p.enforcement})`).join(', '));
}
if (turn.diff.newlyReleased.length > 0) {
console.log('[InPolicy]', 'no longer active:',
turn.diff.newlyReleased.map(p => p.citationRef).join(', '));
}

Use this to surface policy changes in your UI (“This conversation now involves a minor, COPPA applies”) or to write your own audit log.

Post-inference check catches cases where the LLM drifted from the policies even with the injection block. It’s on by default for new API keys. To turn it off for a key, set Settings → API Keys → your key → Settings → Post-inference checking to Disabled; checks with that key then return 403.

Then:

const assistantTurn = await ip.recordTurn({
sessionId,
turn: { role: 'assistant', content: output },
checkOutput: true,
});
const check = assistantTurn.postInferenceCheck;
if (check && !check.checked) {
// Not checked (timeout or outage). `decision` then follows the key's
// on_check_error setting; it is not a clean result.
console.warn('[InPolicy] not checked:', check.uncheckedReason);
} else if (check?.decision === 'block') {
console.warn('[InPolicy] blocked:', check.violations);
// If check.suggestedRedaction is not null, every finding was rewritten and
// you can send it as is. If it is null, regenerate with the violations as
// guidance, or route to a person.
}

A post-inference check takes around 1–2 seconds (see Latency expectations). If your UX can’t wait for it, check asynchronously and act on the check.flagged / check.blocked webhooks.

If your agent has the ability to take actions (send email, trigger workflows, write to DBs), gate those actions too. tool_call_governance is on by default for new API keys; if it is set to disabled, checkToolCall returns 403. The call is checked against your workspace’s policies (scoped by the key’s teams and country), not the conversation’s state.

// Wrap your email-sending tool:
async function guardedSendEmail(sessionId: string, args: { to: string; subject: string }) {
const check = await ip.checkToolCall({
sessionId,
toolName: 'send_email',
arguments: args,
});
if (!check.checked) {
// Not checked: `allow` follows the key's on_check_error, and there are
// no violations to quote.
if (!check.allow) return "Couldn't check that email against policy, so it wasn't sent.";
} else if (!check.allow) {
// Don't execute. Tell the user what policy would be violated.
return `Can't send that — ${check.violations[0]?.explanation ?? 'it breaks a policy'}`;
}
// Proceed with the tool call.
await sendEmail(args);
return 'Sent.';
}
  • API reference — every endpoint, every field.
  • SDK — full @inpolicy/sdk usage.
  • LangChain — drop-in integration if you’re building on LangChain.js.
  • Privacy — what we store, what we don’t.