Guides / Monitoring

Monitor Third-Party API Health

4 min readยทMonitoring

The Problem

Your app is fine. Stripe is having an incident. Your users are getting payment errors. You find out 20 minutes later via Twitter. Logging external API calls gives you the signal earlier.

Step 1: Wrap Your API Clients

ts
// lib/api-monitor.ts
import { logit } from "@/lib/logit";

export async function monitoredCall<T>(
  vendor: string,
  operation: string,
  fn: () => Promise<T>
): Promise<T> {
  const start = Date.now();
  try {
    const result = await fn();
    const duration = Date.now() - start;

    if (duration > 2000) {
      await logit.now("vendor-health", {
        event: "Slow vendor API call",
        description: `${vendor} / ${operation} took ${duration}ms`,
        icon: "๐Ÿข",
        notify: false,
        tags: { vendor, operation, severity: "slow" },
        metadata: { vendor, operation, duration },
      }).catch(console.error);
    }

    return result;
  } catch (err) {
    const duration = Date.now() - start;
    await logit.now("vendor-health", {
      event: "Vendor API error",
      description: `${vendor} / ${operation} failed โ€” ${err instanceof Error ? err.message : String(err)}`,
      icon: "๐Ÿ”ด",
      notify: false,
      tags: { vendor, operation, severity: "error" },
      metadata: { vendor, operation, duration, error: err instanceof Error ? err.message : String(err) },
    }).catch(console.error);
    throw err;
  }
}

Step 2: Use It on Every External Call

ts
const session = await monitoredCall("stripe", "checkout.session.create", () =>
  stripe.checkout.sessions.create({ ... })
);

const completion = await monitoredCall("openai", "chat.completions.create", () =>
  openai.chat.completions.create({ ... })
);

await monitoredCall("sendgrid", "mail.send", () =>
  sgMail.send({ ... })
);

Step 3: Smart Action for Vendor Errors

FieldValue
NameVendor API errors
Channelvendor-health
Event nameVendor API error
Threshold5
Window5 minutes
Cooldown15 minutes
ActionPush notification

Tips

  • Log slow calls (>2s) separately from errors โ€” they're different failure modes.
  • Add vendor as a tag so you can filter the dashboard to only Stripe errors during a Stripe incident.
  • Use catch(console.error) on LogIt calls inside the monitor so logging never blocks production calls.

Try LogIt free

7-day trial. No credit card required.

Start free