Skip to main content
In this guide, you turn an HTML file, such as the output of an HTML editor or a template engine, into a valid request body for an HTML field. Sending HTML in JSON has the following pitfalls:
  • Double quotes in the HTML must be escaped in the JSON string.
  • Editors output a full document with <html> and <body>, but an HTML field holds a fragment.
  • Styles on <body>, such as padding and fonts, are lost unless you move them to a wrapper element.

Before you begin

  • Store your API key in the SAJN_API_KEY environment variable. To create a key, go to workspace settings in the sajn app, then Utvecklare (Developer) > API-nycklar (API keys).
  • Have a DRAFT document. For more information, see Create a document.

Send an HTML file

1

Extract the body and keep its styles

The following functions return the content of <body>, wrapped in a <div> with the body’s style attribute. Content without a <body> tag with attributes is returned unchanged:
For example, the functions turn the following document:
Into the following fragment:
2

Send the fragment as an HTML field

Build the body with your language’s JSON serializer, which escapes the quotes for you. Never build the JSON string by hand. The field goes in a fields array:
Set DOCUMENT_ID in the environment to the ID of the draft document. The response has the created field in data.

Prepare the HTML

  • Use inline style attributes only. sajn removes <style> tags, so styles from a stylesheet or a <head> are lost.
  • Split long content into several HTML fields, ordered by position, instead of one large block.
  • Host images and reference them by https URL instead of inlining them as base64.
For every allowed tag and CSS property, see HTML fields.

Handle errors

  • 400 INVALID_JSON: the body isn’t valid JSON, usually because of unescaped quotes. Serialize the body with JSON.stringify or json.
  • 400 VALIDATION_FAILED on fields.0.fieldMeta.type: type and fieldMeta.type must both be HTML.
For the error shape and every code, see Errors.

Next steps

HTML fields

See the allowed tags and CSS properties.

Send for signing

Send the document to its parties.