@assinafy/sdk 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -34,6 +34,11 @@ interface AssinafyClientOptions {
34
34
  webhookSecret?: string;
35
35
  /** Request timeout in milliseconds. Defaults to 30_000. */
36
36
  timeout?: number;
37
+ /**
38
+ * Max automatic retries on HTTP 429 (rate limit), honoring `Retry-After`.
39
+ * Defaults to `2`. Set to `0` to disable retrying.
40
+ */
41
+ maxRetries?: number;
37
42
  /** Optional logger. Defaults to a no-op logger. */
38
43
  logger?: Logger;
39
44
  }
@@ -70,6 +75,10 @@ interface ISigner {
70
75
  full_name: string;
71
76
  email: string | null;
72
77
  whatsapp_phone_number?: string | null;
78
+ /**
79
+ * Accepted on create/update payloads but **never echoed back** on any signer
80
+ * response — present here only so response objects stay assignable from inputs.
81
+ */
73
82
  cpf?: string | null;
74
83
  has_accepted_terms?: boolean;
75
84
  /** Only returned by `GET /signers/self`. */
@@ -131,23 +140,56 @@ interface ICreateAssignmentPayload {
131
140
  /** Field placement entries used when `method` is `collect`. */
132
141
  entries?: unknown[];
133
142
  }
143
+ /** A signer as embedded inside an assignment (richer than the bare {@link ISigner}). */
144
+ interface IAssignmentSigner extends ISigner {
145
+ completed: boolean;
146
+ notification_history: unknown[];
147
+ verification_method: AssignmentVerificationMethod;
148
+ notification_methods: AssignmentNotificationMethod[];
149
+ /** 1-based signing order. See {@link SignerReference.step}. */
150
+ step: number;
151
+ notified: boolean;
152
+ }
153
+ /** A placed field/item within an assignment (one row per signer × field). */
154
+ interface IAssignmentItem {
155
+ id: string;
156
+ page: {
157
+ id: string;
158
+ number: number;
159
+ height: number;
160
+ width: number;
161
+ download_url: string;
162
+ } | null;
163
+ signer: ISigner;
164
+ field: IFieldDefinition;
165
+ value: string | null;
166
+ completed?: boolean;
167
+ [key: string]: unknown;
168
+ }
134
169
  /** Assignment object as returned by the API. */
135
170
  interface IAssignment {
171
+ resource?: string;
136
172
  id: string;
137
173
  sender_email?: string;
138
174
  method: AssignmentMethod;
139
- expires_at?: string;
175
+ expires_at?: string | null;
140
176
  expiration?: string;
141
177
  message?: string;
142
- signers: ISigner[];
178
+ signers: IAssignmentSigner[];
143
179
  copy_receivers?: string[];
144
- items?: unknown[];
180
+ items?: IAssignmentItem[];
145
181
  summary?: {
146
182
  signer_count: number;
147
183
  completed_count: number;
148
- signers: unknown[];
184
+ signers: Array<ISigner & {
185
+ completed?: boolean;
186
+ }>;
149
187
  };
150
- signing_urls?: Record<string, string>;
188
+ /** Per-signer signing URLs. Array of `{ signer_id, url }` (not a map). */
189
+ signing_urls?: Array<{
190
+ signer_id: string;
191
+ url: string;
192
+ }>;
151
193
  }
152
194
  type ICreateAssignmentResponse = IAssignment;
153
195
  interface IResendEmailResponse {
@@ -155,6 +197,41 @@ interface IResendEmailResponse {
155
197
  document_id?: string;
156
198
  signer_id?: string;
157
199
  }
200
+ /**
201
+ * Credit/document cost estimate returned by `assignments.estimateCost` and
202
+ * `documents.estimateCostFromTemplate`.
203
+ */
204
+ interface ICostEstimate {
205
+ documents: number;
206
+ credits: number;
207
+ needs_extra_document: boolean;
208
+ extra_document_cost: number;
209
+ total_credits: number;
210
+ breakdown: Array<{
211
+ code: string;
212
+ name: string;
213
+ cost: number;
214
+ quantity?: number;
215
+ unit_cost?: number;
216
+ }>;
217
+ document_balance: number;
218
+ credit_balance: number;
219
+ has_sufficient_resources: boolean;
220
+ /** `null` when the operation can proceed; otherwise a reason code. */
221
+ blocking_reason: string | null;
222
+ message: string | null;
223
+ }
224
+ /** Cost estimate returned by `assignments.estimateResendCost`. */
225
+ interface IResendCostEstimate {
226
+ total: number;
227
+ breakdown: Array<{
228
+ code: string;
229
+ name: string;
230
+ cost: number;
231
+ }>;
232
+ credit_balance: number;
233
+ has_sufficient_credits: boolean;
234
+ }
158
235
  /** Webhook payload envelope. */
159
236
  interface IWebhookPayload {
160
237
  id?: number;
@@ -182,6 +259,14 @@ interface IDocumentListItem {
182
259
  status: DocumentStatus;
183
260
  account_id?: string;
184
261
  template_id?: string | null;
262
+ /** Artifact download URLs keyed by name (`original`, `thumbnail`, …). */
263
+ artifacts?: IDocumentUploadResponse['artifacts'];
264
+ /** Public signing-portal URL for the document. */
265
+ signing_url?: string;
266
+ pages?: IDocumentUploadResponse['pages'];
267
+ assignment?: IAssignment | null;
268
+ decline_reason?: string | null;
269
+ declined_by?: ISigner | null;
185
270
  /** Tags attached to the document (inline `{ id, name, color }` shape). */
186
271
  tags?: IInlineTag[];
187
272
  created_at: string;
@@ -206,7 +291,8 @@ interface IDocumentUploadResponse {
206
291
  template_id: string | null;
207
292
  name: string;
208
293
  status: DocumentStatus;
209
- assignment: unknown;
294
+ /** Absent on a fresh upload; an {@link IAssignment} (or `null`) once one exists. */
295
+ assignment?: IAssignment | null;
210
296
  artifacts: {
211
297
  original: string;
212
298
  certificated?: string;
@@ -214,6 +300,7 @@ interface IDocumentUploadResponse {
214
300
  bundle?: string;
215
301
  thumbnail?: string;
216
302
  };
303
+ /** Empty (`[]`) on fresh upload (status `uploaded`); populated once `metadata_ready`. */
217
304
  pages: Array<{
218
305
  id: string;
219
306
  number: number;
@@ -234,6 +321,7 @@ interface IDocumentDetailsResponse {
234
321
  resource?: string;
235
322
  id: string;
236
323
  account_id: string;
324
+ template_id?: string | null;
237
325
  name: string;
238
326
  status: DocumentStatus;
239
327
  assignment: IAssignment | null;
@@ -253,7 +341,7 @@ interface IDocumentDetailsResponse {
253
341
  created_at: string;
254
342
  updated_at: string;
255
343
  is_closed: boolean;
256
- decline_reason?: string;
344
+ decline_reason?: string | null;
257
345
  declined_by?: ISigner | null;
258
346
  activities?: Array<IDocumentActivity>;
259
347
  }
@@ -300,8 +388,8 @@ interface IUpdateWorkspacePayload {
300
388
  interface IWorkspaceResponse {
301
389
  id: string;
302
390
  name: string;
303
- primary_color?: string;
304
- secondary_color?: string;
391
+ primary_color?: string | null;
392
+ secondary_color?: string | null;
305
393
  created_at: string;
306
394
  }
307
395
  interface IWorkspaceListItem {
@@ -319,13 +407,16 @@ interface IWebhookRegisterPayload {
319
407
  events?: WebhookEventType[] | string[];
320
408
  is_active?: boolean;
321
409
  }
410
+ /**
411
+ * Webhook subscription as returned by the API. There is exactly one
412
+ * subscription per workspace, keyed by URL — the API returns
413
+ * `{ events, is_active, url, email, updated_at }` (no `id` / `created_at`).
414
+ */
322
415
  interface IWebhookSubscription {
323
- id?: string;
324
416
  url: string;
325
417
  email: string;
326
418
  events: string[];
327
419
  is_active: boolean;
328
- created_at?: string;
329
420
  updated_at?: string;
330
421
  }
331
422
  interface IWebhookEventTypeInfo {
@@ -372,8 +463,18 @@ interface IUploadAndRequestSignaturesSigner {
372
463
  interface ITemplateRole {
373
464
  id: string;
374
465
  name: string;
466
+ /** Role kind, e.g. `Editor` or `Signer`. */
467
+ assignment_type?: string;
468
+ created_at?: string;
469
+ updated_at?: string;
375
470
  [key: string]: unknown;
376
471
  }
472
+ /** Payload for `PUT /accounts/{id}/templates/{template_id}`. Omit a field to leave it unchanged. */
473
+ interface IUpdateTemplatePayload {
474
+ name?: string;
475
+ /** Default invitation message applied to documents created from the template. */
476
+ message?: string;
477
+ }
377
478
  /** Template list item (paginated). */
378
479
  interface ITemplateListItem {
379
480
  resource?: string;
@@ -613,7 +714,7 @@ declare abstract class BaseResource {
613
714
  }
614
715
  type RequestFn = () => Promise<AxiosResponse>;
615
716
 
616
- /** Input for uploading a document: either an on-disk file or an in-memory buffer. */
717
+ /** Input for an upload: either an on-disk file or an in-memory buffer. */
617
718
  type DocumentUploadSource = {
618
719
  filePath: string;
619
720
  fileName?: string;
@@ -621,6 +722,7 @@ type DocumentUploadSource = {
621
722
  buffer: Buffer;
622
723
  fileName: string;
623
724
  };
725
+
624
726
  interface IDocumentUploadOptions {
625
727
  /** Optional metadata sent alongside the file (JSON-encoded). */
626
728
  metadata?: Record<string, unknown>;
@@ -684,8 +786,13 @@ declare class DocumentResource extends BaseResource {
684
786
  * ```
685
787
  */
686
788
  createFromTemplate(templateId: string, signers: ITemplateSigner[], options?: ICreateDocumentFromTemplateOptions, accountId?: string): Promise<IDocumentDetailsResponse>;
687
- /** Estimate the credit cost of creating a document from a template. */
688
- estimateCostFromTemplate(templateId: string, signers: ITemplateSigner[], accountId?: string): Promise<Record<string, unknown>>;
789
+ /**
790
+ * Estimate the credit cost of creating a document from a template.
791
+ *
792
+ * @returns an {@link ICostEstimate}: `total_credits`, balances, and a
793
+ * per-line `breakdown` of what the operation would consume.
794
+ */
795
+ estimateCostFromTemplate(templateId: string, signers: ITemplateSigner[], accountId?: string): Promise<ICostEstimate>;
689
796
  /** Verify a document by its signature hash. */
690
797
  verify(hash: string): Promise<Record<string, unknown>>;
691
798
  /**
@@ -756,8 +863,16 @@ declare function buildAssignmentPayload(payload: ICreateAssignmentPayload, optio
756
863
  declare class AssignmentResource extends BaseResource {
757
864
  /** Create a signing assignment for a document. */
758
865
  create(documentId: string, payload: ICreateAssignmentPayload): Promise<ICreateAssignmentResponse>;
759
- /** Estimate the cost (in credits) of creating the assignment. */
760
- estimateCost(documentId: string, payload: ICreateAssignmentPayload): Promise<Record<string, unknown>>;
866
+ /**
867
+ * Estimate the cost (in credits/documents) of creating the assignment.
868
+ *
869
+ * Signer entries may omit `id` and supply only `verification_method` /
870
+ * `notification_methods` when only the channel mix matters for the estimate.
871
+ *
872
+ * @returns an {@link ICostEstimate} with `total_credits`, balances, and a
873
+ * line-item `breakdown`.
874
+ */
875
+ estimateCost(documentId: string, payload: ICreateAssignmentPayload): Promise<ICostEstimate>;
761
876
  /**
762
877
  * Update the expiration date of an existing assignment.
763
878
  * Pass `null` to remove the expiration entirely.
@@ -765,22 +880,40 @@ declare class AssignmentResource extends BaseResource {
765
880
  resetExpiration(documentId: string, assignmentId: string, expiresAt: string | null): Promise<IAssignment>;
766
881
  /** Resend the signing notification to a single signer. */
767
882
  resendNotification(documentId: string, assignmentId: string, signerId: string): Promise<IResendEmailResponse>;
768
- /** Estimate the cost of resending a signer notification. */
769
- estimateResendCost(documentId: string, assignmentId: string, signerId: string): Promise<Record<string, unknown>>;
883
+ /**
884
+ * Estimate the cost of resending a signer notification.
885
+ *
886
+ * @returns an {@link IResendCostEstimate} (`total`, `breakdown`, balances).
887
+ */
888
+ estimateResendCost(documentId: string, assignmentId: string, signerId: string): Promise<IResendCostEstimate>;
770
889
  /**
771
890
  * `GET /documents/{documentId}/assignments/{assignmentId}/whatsapp-notifications`
772
891
  * — list every WhatsApp notification rendered + sent for an assignment.
773
892
  */
774
893
  listWhatsAppNotifications(documentId: string, assignmentId: string): Promise<IWhatsAppNotification[]>;
775
- /**
776
- * Cancel a signature request. This endpoint is not listed in the public
777
- * Swagger but is exposed by the platform.
778
- */
779
- cancel(documentId: string, reason: string, accountId?: string): Promise<unknown>;
780
894
  }
781
895
 
896
+ /**
897
+ * Default webhook events applied by {@link WebhookResource.register} when the
898
+ * caller omits `events` (or passes an empty array).
899
+ */
900
+ declare const DEFAULT_WEBHOOK_EVENTS: WebhookEventType[];
782
901
  declare class WebhookResource extends BaseResource {
783
- /** Register (or replace) the webhook subscription for the workspace. */
902
+ /**
903
+ * Register (or replace) the workspace's single webhook subscription
904
+ * (`PUT /accounts/{id}/webhooks/subscriptions`). There is exactly one
905
+ * subscription per workspace, keyed by URL.
906
+ *
907
+ * When `events` is omitted or empty, {@link DEFAULT_WEBHOOK_EVENTS} is used
908
+ * (`document_ready`, `document_prepared`, `signer_signed_document`,
909
+ * `signer_rejected_document`, `document_processing_failed`).
910
+ *
911
+ * @example
912
+ * ```ts
913
+ * await client.webhooks.register({ url: 'https://example.com/hook', email: 'ops@example.com' });
914
+ * // → { url, email, events: [...], is_active: true, updated_at: '2026-…' }
915
+ * ```
916
+ */
784
917
  register(payload: IWebhookRegisterPayload, accountId?: string): Promise<IWebhookSubscription>;
785
918
  /** Fetch the current webhook subscription. Returns `null` if none exists. */
786
919
  get(accountId?: string): Promise<IWebhookSubscription | null>;
@@ -797,20 +930,55 @@ declare class WebhookResource extends BaseResource {
797
930
  }
798
931
 
799
932
  declare class TemplateResource extends BaseResource {
933
+ /**
934
+ * Create a template by uploading a PDF (`POST /accounts/{id}/templates`).
935
+ *
936
+ * The template is created in `Uploaded` status and transitions to `Ready`
937
+ * once the platform finishes processing its pages. Configure roles/fields
938
+ * afterwards in the Assinafy editor.
939
+ *
940
+ * @example
941
+ * ```ts
942
+ * const tmpl = await client.templates.create(
943
+ * { filePath: './nda.pdf' },
944
+ * { name: 'NDA template' },
945
+ * );
946
+ * // → { resource: 'template', id, name, status: 'Uploaded',
947
+ * // roles: [{ id, name: 'TemplateEditor', assignment_type: 'Editor' }],
948
+ * // pages: [], tags: [], created_at, updated_at }
949
+ * ```
950
+ */
951
+ create(source: DocumentUploadSource, options?: {
952
+ name?: string;
953
+ accountId?: string;
954
+ }): Promise<ITemplateDetailsResponse>;
800
955
  /** List templates for the workspace. */
801
956
  list(params?: IListParams, accountId?: string): Promise<ITemplateListResponse>;
802
957
  /**
803
- * Get a template by ID.
958
+ * Get a template by ID (`GET /accounts/{id}/templates/{template_id}`).
804
959
  *
805
- * Note: the swagger only documents the list endpoint; this single-resource
806
- * `GET /accounts/{id}/templates/{id}` is exposed by the platform and used
807
- * by the official PHP SDK.
960
+ * Unlike the list endpoint, the single-template response includes `pages`
961
+ * (with per-page `download_url`) and `default_document_tags`.
808
962
  */
809
963
  get(templateId: string, accountId?: string): Promise<ITemplateDetailsResponse>;
810
964
  /**
811
- * `GET /accounts/{id}/templates/{template_id}/pages/{page_id}/download`
812
- * download a template page as a JPEG (used by template editors to render
813
- * thumbnails on the client).
965
+ * Update a template's `name` and/or default `message`
966
+ * (`PUT /accounts/{id}/templates/{template_id}`). Returns the updated template.
967
+ *
968
+ * @example
969
+ * ```ts
970
+ * await client.templates.update(templateId, { name: 'NDA v2', message: 'Please sign' });
971
+ * ```
972
+ */
973
+ update(templateId: string, payload: IUpdateTemplatePayload, accountId?: string): Promise<ITemplateDetailsResponse>;
974
+ /** Delete a template (`DELETE /accounts/{id}/templates/{template_id}`). */
975
+ delete(templateId: string, accountId?: string): Promise<void>;
976
+ /**
977
+ * Download a template page as a JPEG
978
+ * (`GET /accounts/{id}/templates/{template_id}/pages/{page_id}/download`).
979
+ *
980
+ * Used by template editors to render page thumbnails on the client. The
981
+ * matching `download_url` is also returned on each `template.pages[]` entry.
814
982
  */
815
983
  downloadPage(templateId: string, pageId: string, accountId?: string): Promise<Buffer>;
816
984
  }
@@ -990,14 +1158,19 @@ declare class SignerDocumentsResource extends BaseResource {
990
1158
  }): Promise<unknown>;
991
1159
  /** `GET /signature/{type}?signer-access-code=…` — download the signer's signature/initial. */
992
1160
  downloadSignature(signerAccessCode: string, imageType?: 'signature' | 'initial'): Promise<Buffer>;
993
- /** `GET /sign?signer-access-code=…` — fetch the assignment as the signer sees it. */
1161
+ /**
1162
+ * `GET /sign?signer-access-code=…` — fetch the assignment as the signer sees it.
1163
+ *
1164
+ * @param hasAcceptedTerms maps to the `has_accepted_terms` query param
1165
+ * (server default `false`); pass `true` once the signer has accepted terms.
1166
+ */
994
1167
  getAssignment(signerAccessCode: string, hasAcceptedTerms?: boolean): Promise<unknown>;
995
1168
  /** `POST /documents/{documentId}/assignments/{assignmentId}?signer-access-code=…` — sign. */
996
1169
  sign(documentId: string, assignmentId: string, signerAccessCode: string, entries: ISignFieldEntry[]): Promise<unknown>;
997
1170
  /**
998
1171
  * `PUT /documents/{documentId}/assignments/{assignmentId}/reject?signer-access-code=…`
999
- * — signer-side decline. (Distinct from `assignments.cancel`, which is the
1000
- * workspace-side cancellation flow.)
1172
+ * — signer-side decline. (The workspace-side equivalent is to delete the
1173
+ * document via `documents.delete`; there is no workspace "cancel" endpoint.)
1001
1174
  */
1002
1175
  decline(documentId: string, assignmentId: string, signerAccessCode: string, declineReason: string): Promise<unknown>;
1003
1176
  }
@@ -1037,6 +1210,7 @@ interface ClientConfigInput {
1037
1210
  webhook_secret?: string;
1038
1211
  webhookSecret?: string;
1039
1212
  timeout?: number;
1213
+ maxRetries?: number;
1040
1214
  logger?: Logger;
1041
1215
  }
1042
1216
  /**
@@ -1120,4 +1294,4 @@ declare class NetworkError extends AssinafyError {
1120
1294
  });
1121
1295
  }
1122
1296
 
1123
- export { ApiError, type AssignmentMethod, type AssignmentNotificationMethod, AssignmentResource, type AssignmentVerificationMethod, AssinafyClient, type AssinafyClientOptions, AssinafyError, AuthenticationResource, type ClientConfigInput, type DocumentArtifactName, DocumentResource, type DocumentStatus, type DocumentUploadSource, FieldsResource, type IApiKeyResponse, type IAssignment, type ICreateAssignmentPayload, type ICreateAssignmentResponse, type ICreateDocumentFromTemplateOptions, type ICreateFieldPayload, type ICreateSignerPayload, type ICreateSignerResponse, type ICreateTagPayload, type ICreateWorkspacePayload, type IDocumentActivity, type IDocumentDetailsResponse, type IDocumentListItem, type IDocumentListParams, type IDocumentListResponse, type IDocumentStatusInfo, type IDocumentUploadOptions, type IDocumentUploadResponse, type IFieldDefinition, type IFieldType, type IFieldValidateMultipleEntry, type IFieldValidationResult, type IInlineTag, type IListParams, type ILoginResponse, type IMaskedApiKeyResponse, type IPaginatedResponse, type IPublicDocumentInfo, type IResendEmailResponse, type ISignFieldEntry, type ISigner, type ISignerListResponse, type ISigningProgress, type ITag, type ITemplateDetailsResponse, type ITemplateListItem, type ITemplateListResponse, type ITemplateRole, type ITemplateSigner, type IUpdateFieldPayload, type IUpdateSignerPayload, type IUpdateTagPayload, type IUpdateWorkspacePayload, type IUploadAndRequestSignaturesResult, type IUploadAndRequestSignaturesSigner, type IWebhookDispatch, type IWebhookDispatchListParams, type IWebhookEventTypeInfo, type IWebhookPayload, type IWebhookRegisterPayload, type IWebhookSubscription, type IWhatsAppNotification, type IWorkspaceListItem, type IWorkspaceListResponse, type IWorkspaceResponse, type Logger, NetworkError, type PaginatedResult, type PaginationMeta, type SendTokenChannel, SignerDocumentsResource, type SignerReference, SignerResource, TagResource, TemplateResource, ValidationError, type WebhookEventType, WebhookResource, WebhookVerifier, WorkspaceResource, buildAssignmentPayload };
1297
+ export { ApiError, type AssignmentMethod, type AssignmentNotificationMethod, AssignmentResource, type AssignmentVerificationMethod, AssinafyClient, type AssinafyClientOptions, AssinafyError, AuthenticationResource, type ClientConfigInput, DEFAULT_WEBHOOK_EVENTS, type DocumentArtifactName, DocumentResource, type DocumentStatus, type DocumentUploadSource, FieldsResource, type IApiKeyResponse, type IAssignment, type IAssignmentItem, type IAssignmentSigner, type ICostEstimate, type ICreateAssignmentPayload, type ICreateAssignmentResponse, type ICreateDocumentFromTemplateOptions, type ICreateFieldPayload, type ICreateSignerPayload, type ICreateSignerResponse, type ICreateTagPayload, type ICreateWorkspacePayload, type IDocumentActivity, type IDocumentDetailsResponse, type IDocumentListItem, type IDocumentListParams, type IDocumentListResponse, type IDocumentStatusInfo, type IDocumentUploadOptions, type IDocumentUploadResponse, type IFieldDefinition, type IFieldType, type IFieldValidateMultipleEntry, type IFieldValidationResult, type IInlineTag, type IListParams, type ILoginResponse, type IMaskedApiKeyResponse, type IPaginatedResponse, type IPublicDocumentInfo, type IResendCostEstimate, type IResendEmailResponse, type ISignFieldEntry, type ISigner, type ISignerListResponse, type ISigningProgress, type ITag, type ITemplateDetailsResponse, type ITemplateListItem, type ITemplateListResponse, type ITemplateRole, type ITemplateSigner, type IUpdateFieldPayload, type IUpdateSignerPayload, type IUpdateTagPayload, type IUpdateTemplatePayload, type IUpdateWorkspacePayload, type IUploadAndRequestSignaturesResult, type IUploadAndRequestSignaturesSigner, type IWebhookDispatch, type IWebhookDispatchListParams, type IWebhookEventTypeInfo, type IWebhookPayload, type IWebhookRegisterPayload, type IWebhookSubscription, type IWhatsAppNotification, type IWorkspaceListItem, type IWorkspaceListResponse, type IWorkspaceResponse, type Logger, NetworkError, type PaginatedResult, type PaginationMeta, type SendTokenChannel, SignerDocumentsResource, type SignerReference, SignerResource, TagResource, TemplateResource, ValidationError, type WebhookEventType, WebhookResource, WebhookVerifier, WorkspaceResource, buildAssignmentPayload };