Skip to content

Partner webhooks

A partner registers a single webhook endpoint per environment that receives events for all of its managed tenants — no per-tenant registration. This is the push channel that replaces polling across the whole fleet.

Ventana de terminal
curl -X POST https://api.emite.cr/v1/partner/webhooks \
-H "x-partner-key: $EMITECR_PARTNER_KEY" \
-H "Content-Type: application/json" \
-d '{
"environment": "production",
"url": "https://pos-cloud.example.com/webhooks/emitecr",
"events": ["invoice.submitted", "invoice.accepted", "invoice.rejected", "invoice.error",
"credentials.rejected", "credentials.restored"]
}'

The response includes a secret (whsec_…) shown once → your vault. GET /v1/partner/webhooks lists your endpoints, POST /v1/partner/webhooks/:webhookId/rotate-secret gives one a new secret and DELETE /v1/partner/webhooks/:webhookId removes one.

The same calls from the SDK:

import { EmitecrPartnerClient, PARTNER_WEBHOOK_EVENTS } from '@emitecr/sdk/partner';
const partner = new EmitecrPartnerClient({ partnerKey: process.env.EMITECR_PARTNER_KEY! });
const existing = await partner.webhooks.list(); // check before registering
const endpoint = await partner.webhooks.register({
environment: 'production',
url: 'https://pos-cloud.example.com/webhooks/emitecr',
events: [...PARTNER_WEBHOOK_EVENTS],
});
// endpoint.secret is shown exactly once.
const rotated = await partner.webhooks.rotateSecret(endpoint.id); // the previous one signs for 24 more hours
await partner.webhooks.remove(endpoint.id);
  • Register once per EmiteCR deployment you integrate with, and per environment you need. Each deployment (its own API base URL, such as a staging API next to the production one) has its own partner credential and its own endpoints. Sandbox and production are not deployments, but each endpoint receives the events of one environment: the one environment says when it is registered, which is required and cannot be changed. To receive both, register one in each environment; each has its own secret. A production one requires your partner app to have production access (403 PARTNER_PRODUCTION_NOT_ENABLED). Registering the same URL twice in one environment doubles every delivery under two different secrets; your receiver can verify only one of them, so the other endpoint fails until it is paused and, days later, disabled. List first.
  • Health lives on the endpoint: status, lastSuccessAt, consecutiveFailures and failingSince. After 10 failed attempts in a row it is paused: its events wait (heldDeliveries) and we try it from time to time (nextProbeAt) until it responds; after 7 days without a success it is disabled. Nobody on your side gets an email — a partner has no members to write to — so watch status from your own monitoring. The EmiteCR team watches it too: it sees your endpoints, tests them and redelivers deliveries, and a test that gets through brings a disabled one back.
  • The secret is rotated without changing endpoints. With POST /v1/partner/webhooks/:webhookId/rotate-secret, for 24 hours every delivery is signed with the previous secret and the new one: put the new one in your receiver within that window and nothing is lost. Until those 24 hours are over it cannot be rotated again.
  • An endpoint is not edited. To change the URL or the events, go in this order: register the new endpoint in the same environment, put its secret in your receiver, THEN remove the old one. While both exist every event is delivered to each, so whichever secret your receiver holds, one copy verifies (dedup by event.id absorbs the other). Removing first opens a gap: pending deliveries of a removed endpoint are dropped, not rerouted.

Everything else is the same as tenant webhooks

Section titled “Everything else is the same as tenant webhooks”

The payload shape, signature scheme (X-Emitecr-Signature, and the deprecated headers before it), verification with verifyWebhook, SSRF rules, idempotent consumption (dedup by event.id, upsert by newest occurredAt), retries, pause and disable are identical to tenant webhooks. The partner plane has no test event (POST /v1/webhooks/:id/test) nor delivery history with its resend (GET /v1/webhooks/:id/deliveries): for a partner endpoint the EmiteCR team uses them, and its test arrives as webhook.test with tenantId set to null, since the endpoint receives the events of all your tenants. Answer it with a 2xx without routing it.

The one difference to plan for: the events of many tenants arrive on one endpoint per environment. Every event carries tenantId and environment: route by tenantId to the managed tenant. If you receive both environments, verify each delivery with the secret of the endpoint of its environment: verifyWebhook with { environment } rejects an event of the other one, so the sandbox secret never vouches for a production event. Beyond that, it depends on the kind of event:

  • invoice.*: find the document by data.externalReference (your business id; null when the invoice carries none) or by data.invoiceId.
  • credentials.*: there is no document — data holds only rejectedAt and detail. The event is about the tenant’s ATV credentials in that environment, so key it by (tenantId, environment). A credentials.restored carries the same data.rejectedAt as the credentials.rejected it ends.

credentials.rejected matters more to a partner than to anyone. A managed tenant has no members, so nobody receives the notice email: when Hacienda refuses a customer’s ATV credentials, this event is the only signal. Their documents keep their number and wait — nothing is lost — until the credentials are updated through the partner API; then they go out on their own and credentials.restored follows. PARTNER_WEBHOOK_EVENTS includes both types, so a receiver must branch on event.type before reading event.data (CredentialsWebhookData vs InvoiceWebhookData in @emitecr/sdk/webhooks). An endpoint registered before these types existed does not receive them: replace it following the order above. The same state is readable without the webhook, in the tenant inventory (readiness).

import { verifyWebhook } from '@emitecr/sdk/webhooks';
// One secret per environment: each endpoint has its own.
const SECRETS = [
['production', process.env.EMITECR_WEBHOOK_SECRET],
['sandbox', process.env.EMITECR_WEBHOOK_SANDBOX_SECRET],
] as const;
app.post('/webhooks/emitecr', express.raw({ type: 'application/json' }), (req, res) => {
let event;
for (const [environment, secret] of SECRETS) {
if (!secret) continue;
try {
// An event of the other environment is rejected: the sandbox secret never vouches for production.
event = verifyWebhook(req.body, req.headers, secret, { environment });
break;
} catch {
// Not this secret: try the next one.
}
}
if (!event) return res.status(400).end();
// A test from the EmiteCR team belongs to no tenant: its tenantId is null.
if (event.type === 'webhook.test') return res.status(200).end();
// event.tenantId tells you WHICH managed tenant this is for.
routeToTenant(event.tenantId, event);
res.status(200).end();
});

Without the SDK, verify with the plain recipe in Webhooks → Verify the signature.