Before you begin
- Store your API key in the
SAJN_API_KEYenvironment variable. To create a key, go to workspace settings in the sajn app, then Utvecklare (Developer) > API-nycklar (API keys). - Have a
DRAFTdocument. For more information, see Create a document. - Have a file of at most 25 MB (26,214,400 bytes) in a supported format. sajn accepts PDF, Word, Excel, and PowerPoint documents; JPEG, PNG, GIF, WebP, and SVG images; and MP4, QuickTime, AVI, and WebM video. For the exact MIME types, see Create a file.
How the upload works
- You create a file record with the file’s name, MIME type, size, and SHA-256 checksum. The response is the file, with its
id, a storagekey, and anuploadUrl. - You send the bytes to
uploadUrlin aPUTrequest. Storage rejects bytes that don’t match the checksum. - You confirm the upload, so sajn verifies the stored bytes.
- You reference the storage
keyin a document field.
Upload a file and attach it
1
Compute the checksum
The checksum is the SHA-256 hash of the exact bytes you upload, encoded as base64:
2
Create the file record
Send a The response is the file, in the same shape as Get file by ID, plus
POST request to /api/v1/files:key and uploadUrl. The following example leaves out url and uploadedBy:uploadUrl is valid for 15 minutes. visibility is optional: PRIVATE, the default, serves the file only through short-lived signed URLs, and PUBLIC serves it from a permanent URL.3
Upload the bytes
Send the file to Replace
uploadUrl with the same Content-Type and the checksum in the x-amz-checksum-sha256 header. Don’t send your Authorization header; the URL is already signed:UPLOAD_URL with the uploadUrl from the previous step. A successful upload returns an empty 200 response.4
Confirm the upload
Confirm the file, so sajn verifies the stored size and checksum before you use it:Replace
FILE_ID with the file id. sajn checks the stored size and, when storage records it, the checksum. The response is the file, in the same shape as Get file by ID.Confirming is idempotent. If you skip it, sajn confirms the file the first time you call Get file by ID.5
Attach the file to the document
Create a Replace the following:
PDF field whose fieldMeta.value is the storage key:DOCUMENT_ID: the ID of the draft document.STORAGE_KEY: thekeyfrom the file record.
data, in the same shape as Get a document field. To replace the PDF of an existing field, send a PATCH request to /api/v1/documents/DOCUMENT_ID/fields/FIELD_ID with the full fieldMeta.To place signature boxes and input fields on the PDF’s pages, see Place fields on a PDF.Complete example
The following programs run the whole flow forcontract.pdf. Set SAJN_API_KEY and DOCUMENT_ID in the environment first:
Handle errors
400 VALIDATION_FAILEDonPOST /api/v1/files: themimeTypeisn’t supported, orsizeis more than 26,214,400 bytes. Theissuesarray names the field.403from theuploadUrl: the URL expired, or theContent-Typedoesn’t matchmimeType. Create a new file record.400from theuploadUrl: the bytes don’t match the checksum. Compute the size and the checksum from the same bytes you upload.401 UNAUTHORIZEDon a sajn endpoint: theAuthorizationheader is missing or the key is invalid. TheuploadUrldoesn’t take the header.
Next steps
Place fields on a PDF
Add signature boxes and inputs to the uploaded PDF.
Send for signing
Send the document to its parties.
Create a file
See the endpoint in the API reference.
Confirm an uploaded file
See the endpoint in the API reference.

