APIサーバー設定
Node.js以外の言語(Python・Go・Rubyなど)から使いたい場合や、複数のマイクロサービスから共通で呼び出したい場合向けです。
Node.jsアプリに直接SDKを組み込む場合はコア関数リファレンスだけで十分です。
Expressミドルウェアパターン
javascript
const express = require("express");
const { createFirewall } = require("@pii-firewall/sdk");
const app = express();
const fw = createFirewall();
app.use(express.json());
// ── これだけで /ai エンドポイントにPII保護が追加される ──
app.use("/ai", fw.expressMiddleware());
app.post("/ai", async (req, res) => {
// req.body.text はすでにマスク済みでここに届く
const aiResponse = await callYourAI(req.body.text);
res.json({ answer: aiResponse });
});スタンドアロンAPIサーバー
Python・Go・Rubyなど他言語からHTTPで呼び出す構成です。
javascript
// pii-server.js
const express = require("express");
const { createFirewall } = require("@pii-firewall/sdk");
const app = express();
const fw = createFirewall({ extraTypes: ["name"] });
app.use(express.json());
app.post("/mask", (req, res) => res.json(fw.mask(req.body.text)));
app.post("/detect", (req, res) => res.json({ detections: fw.detect(req.body.text) }));
app.post("/restore", (req, res) => res.json({ restored: fw.restoreAll(req.body.text) }));
app.post("/check-injection", (req, res) => res.json({ threats: fw.detectInjection(req.body.text) }));
app.listen(7777, () => console.log("PII Firewall API: http://localhost:7777"));bash
node pii-server.jsPythonから呼び出す例:
python
import requests
# マスク
r = requests.post("http://localhost:7777/mask", json={"text": "田中太郎 tanaka@example.com"})
masked = r.json()["masked"]
# AIに送る(masked を使う)
ai_response = call_your_ai(masked)
# 復元
r2 = requests.post("http://localhost:7777/restore", json={"text": ai_response})
restored = r2.json()["restored"]推奨エンドポイント設計
| エンドポイント | メソッド | 用途 |
|---|---|---|
/v1/mask | POST | PIIをトークン化して返す |
/v1/detect | POST | PIIを検知するだけ(マスクなし) |
/v1/restore | POST | トークンを元の値に復元 |
/v1/secure | POST | インジェクション検知+マスクを1回で実行(推奨) |
/v1/status | GET | サーバー稼働確認+トークン保存数 |
/v1/secure(推奨エンドポイント)の実装例
javascript
app.post("/v1/secure", (req, res) => {
const { text } = req.body;
const threats = fw.detectInjection(text);
if (threats.some(t => t.level === "critical")) {
return res.status(403).json({ ok: false, reason: "injection_blocked", threats });
}
const { masked, detections } = fw.mask(text);
res.json({
ok: true,
masked,
pii_count: detections.length,
pii_types: [...new Set(detections.map(d => d.type))],
});
});
app.get("/v1/status", (req, res) => {
const store = fw.storeStatus();
res.json({ status: "ok", active_tokens: store.length });
});セキュリティのベストプラクティス
javascript
// 1. APIキー認証を追加する(外部公開する場合)
app.use("/v1", (req, res, next) => {
const key = req.headers["x-api-key"];
if (key !== process.env.PII_FIREWALL_API_KEY) {
return res.status(401).json({ error: "Unauthorized" });
}
next();
});
// 2. レートリミットを設ける
const rateLimit = require("express-rate-limit");
app.use("/v1", rateLimit({ windowMs: 60_000, max: 100 }));
// 3. ログに元のPIIを含めない
app.post("/v1/mask", (req, res) => {
const { masked, detections } = fw.mask(req.body.text);
console.log(`[mask] ${detections.length} items detected`); // 件数だけログ
// console.log(req.body.text) ← ❌ 元のテキストをログに残さない
res.json({ masked, detections });
});Enterpriseプランをご検討の方へ
社内LLM・オンプレミス環境・FISC / HIPAA / ISO対応が必要な場合は、Enterpriseプランをご覧ください。カスタムPIIルール・要配慮個人情報対応・SLA 99.9%に対応しています。
