Quick Start
import { Steps, Tabs, TabItem } from ‘@astrojs/starlight/components’;
Quick Start
Get ContractShield protecting your API in under 5 minutes.
-
Install the packages
```bash npm install @cshield/pdp @cshield/pep-express ``` ```bash npm install @cshield/pdp @cshield/pep-fastify ``` -
Create a policy file
Create
policy.yamlin your project root:policy.yaml policyVersion: "0.1"defaults:mode: enforceunmatchedRouteAction: allowvulnerabilityChecks:prototypePollution: truepathTraversal: trueroutes:- id: healthmatch:method: GETpath: /health- id: orders.creatematch:method: POSTpath: /api/orderscontract:requestSchema:type: objectproperties:items:type: arrayminItems: 1items:type: objectproperties:productId: { type: string }quantity: { type: integer, minimum: 1 }required: [productId, quantity]customerId:type: stringrequired: [items, customerId]rules:- id: max-itemstype: celaction: blockconfig:expr: "size(request.body.items) <= 50"message: "Order cannot exceed 50 items" -
Add the middleware
```javascript import express from 'express'; import { contractshield } from '@cshield/pep-express'; const app = express();app.use(express.json());// Add ContractShield middlewareapp.use(contractshield({policy: './policy.yaml'}));// Your routesapp.get('/health', (req, res) => {res.json({ status: 'ok' });});app.post('/api/orders', (req, res) => {// Request has been validated by ContractShieldconst { items, customerId } = req.body;res.json({ orderId: crypto.randomUUID() });});app.listen(3000, () => {console.log('Server running on http://localhost:3000');});``````javascript import Fastify from 'fastify'; import { contractshieldPlugin } from '@cshield/pep-fastify'; const app = Fastify();// Register ContractShield pluginawait app.register(contractshieldPlugin, {policy: './policy.yaml'});// Your routesapp.get('/health', async () => {return { status: 'ok' };});app.post('/api/orders', async (request) => {const { items, customerId } = request.body;return { orderId: crypto.randomUUID() };});await app.listen({ port: 3000 });``` -
Test it
Terminal window # Valid request - should succeedcurl -X POST http://localhost:3000/api/orders \-H "Content-Type: application/json" \-d '{"items": [{"productId": "SKU-123", "quantity": 2}], "customerId": "cust_1"}'# Invalid request - should be blocked (negative quantity)curl -X POST http://localhost:3000/api/orders \-H "Content-Type: application/json" \-d '{"items": [{"productId": "SKU-123", "quantity": -5}], "customerId": "cust_1"}'
What Just Happened?
- Policy loaded: ContractShield parsed your
policy.yamland compiled the validation rules - Request validated: Each incoming request is checked against:
- Vulnerability patterns (prototype pollution, path traversal)
- JSON Schema contract (structure, types, constraints)
- CEL rules (business logic invariants)
- Decision made:
ALLOWorBLOCKwith detailed reason
Access the Decision
You can access the ContractShield decision in your route handlers:
app.post('/api/orders', (req, res) => { const { decision, context } = req.contractshield;
console.log(decision.action); // "ALLOW" console.log(decision.risk.score); // 0-100 console.log(decision.ruleHits); // Array of triggered rules
// Your logic here});