Skip to main content
TRUSTORYX.
એન્ટરપ્રાઇઝ આર્કિટેક્ચર માર્ગદર્શિકા

એન્ટરપ્રાઇઝ API અને વેબહુક એકીકરણ આયોજન માર્ગદર્શિકા

ફોલ્ટ-ટોલરન્ટ API ઇન્ટિગ્રેશન બનાવતા સોફ્ટવેર આર્કિટેક્ટ્સ માટે વિગતવાર એન્જિનિયરિંગ માર્ગદર્શિકા. ક્રિપ્ટોગ્રાફિક HMAC, રેડિસ આઇડેમ્પોટેન્સી અને કતારો.

સુરક્ષિત ક્રિપ્ટોગ્રાફી ડિસ્ટ્રિબ્યુટેડ આઇડેમ્પોટેન્સી n8n અને Node.js પેટર્ન

૧. HMAC SHA-256 સહીઓ

અપ્રમાણિત વેબહુક્સ પર ક્યારેય વિશ્વાસ ન કરો. ટાઇમિંગ હુમલા અટકાવવા સુરક્ષિત સરખામણી દ્વારા પેલોડ ચકાસો.

૨. ડિસ્ટ્રિબ્યુટેડ આઇડેમ્પોટેન્સી

Redis SETNX લૉક્સ ડુપ્લિકેટ ચૂકવણી અને ડેટાબેઝ ભૂલો અટકાવે છે.

૩. અસિંક્રોનસ ડીકપલિંગ

હંમેશાં HTTP 202 સાથે તરત જ સ્વીકારો. ગેટવે ટાઈમઆઉટ ટાળવા પ્રોસેસિંગને વર્કર કતારમાં મોકલો.

// Next.js 16 App Router: Idempotent Webhook Consumer with Timing-Safe HMAC
import { NextRequest, NextResponse } from 'next/server';
import crypto from 'crypto';

const WEBHOOK_SECRET = process.env.STRIPE_OR_PARTNER_SECRET!;

export async function POST(req: NextRequest) {
  try {
    const rawBody = await req.text();
    const signature = req.headers.get('x-signature') || '';
    const idempotencyKey = req.headers.get('idempotency-key');

    if (!idempotencyKey) {
      return NextResponse.json({ error: 'Missing Idempotency-Key header' }, { status: 400 });
    }

    // 1. Timing-Safe HMAC Verification
    const expectedSig = crypto
      .createHmac('sha256', WEBHOOK_SECRET)
      .update(rawBody)
      .digest('hex');

    const isValid = crypto.timingSafeEqual(
      Buffer.from(signature, 'hex'),
      Buffer.from(expectedSig, 'hex')
    );

    if (!isValid) {
      return NextResponse.json({ error: 'Invalid HMAC Signature' }, { status: 401 });
    }

    // 2. Distributed Redis Idempotency Check (Atomic SETNX with 24h TTL)
    // const isNew = await redis.set(`idempotency:${idempotencyKey}`, 'processing', 'EX', 86400, 'NX');
    // if (!isNew) return NextResponse.json({ status: 'already_processed' }, { status: 200 });

    // 3. Immediately acknowledge HTTP 202 and push to background worker queue
    // await jobQueue.add('process_event', { payload: JSON.parse(rawBody) });

    return NextResponse.json({ received: true, status: 'queued' }, { status: 202 });
  } catch (error) {
    return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
  }
}
એન્ટરપ્રાઇઝ ઇન્ટિગ્રેશન એન્જિનિયરિંગની જરૂર છે?

તમારા CRM, ERP અને AI સિસ્ટમ્સને ભૂલો વિના જોડો

Trustoryx મજબૂત API પાઇપલાઇન્સ અને ઓટોમેશન વર્કફ્લો ડિઝાઇન કરે છે.