Skip to content

Quick Start

import { Steps, Tabs, TabItem } from ‘@astrojs/starlight/components’;

Quick Start

Get ContractShield protecting your API in under 5 minutes.

  1. Install the packages

    ```bash npm install @cshield/pdp @cshield/pep-express ``` ```bash npm install @cshield/pdp @cshield/pep-fastify ```
  2. Create a policy file

    Create policy.yaml in your project root:

    policy.yaml
    policyVersion: "0.1"
    defaults:
    mode: enforce
    unmatchedRouteAction: allow
    vulnerabilityChecks:
    prototypePollution: true
    pathTraversal: true
    routes:
    - id: health
    match:
    method: GET
    path: /health
    - id: orders.create
    match:
    method: POST
    path: /api/orders
    contract:
    requestSchema:
    type: object
    properties:
    items:
    type: array
    minItems: 1
    items:
    type: object
    properties:
    productId: { type: string }
    quantity: { type: integer, minimum: 1 }
    required: [productId, quantity]
    customerId:
    type: string
    required: [items, customerId]
    rules:
    - id: max-items
    type: cel
    action: block
    config:
    expr: "size(request.body.items) <= 50"
    message: "Order cannot exceed 50 items"
  3. Add the middleware

    ```javascript import express from 'express'; import { contractshield } from '@cshield/pep-express';
    const app = express();
    app.use(express.json());
    // Add ContractShield middleware
    app.use(contractshield({
    policy: './policy.yaml'
    }));
    // Your routes
    app.get('/health', (req, res) => {
    res.json({ status: 'ok' });
    });
    app.post('/api/orders', (req, res) => {
    // Request has been validated by ContractShield
    const { 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 plugin
    await app.register(contractshieldPlugin, {
    policy: './policy.yaml'
    });
    // Your routes
    app.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 });
    ```
  4. Test it

    Terminal window
    # Valid request - should succeed
    curl -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?

  1. Policy loaded: ContractShield parsed your policy.yaml and compiled the validation rules
  2. 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)
  3. Decision made: ALLOW or BLOCK with 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
});

Next Steps