You are viewing the documentation for Digipost API Client 13.x, which is not the most recently released version. The newest version is 16.x and can be browsed here.
This version is deprecated. As of dec 2023 we will only support v16 and higher since we now only support Java >= 11 with jakarta.xml-bind.

Instantiate and configure the client

Configure for production use

To instantiate the client instance you need to supply your assigned broker ID, which is set up to be permitted to integrate with the Digipost API. In addition, you must create a Signer instance, e.g. by using a .p12 file to read the private key to use for signing the API requests.

SenderId senderId = SenderId.of(123456);

Signer signer;
try (InputStream sertifikatInputStream = Files.newInputStream(Paths.get("certificate.p12"))) {
    signer = Signer.usingKeyFromPKCS12KeyStore(sertifikatInputStream, "TheSecretPassword");
}

DigipostClient client = new DigipostClient(
        DigipostClientConfig.newConfiguration().build(), senderId.asBrokerId(), signer);

This example will configure the client to communicate with the regular Digipost production environment.

Other environments

If you have access to other environments, this can be configured using DigipostClientConfig, e.g:

URI apiUri = URI.create("https://api.test.digipost.no");
DigipostClientConfig config = DigipostClientConfig.newConfiguration().digipostApiUri(apiUri).build();

Norsk Helsenett (NHN)

The Digipost API is accessible from both internet and Norsk Helsenett (NHN). Both entry points use the same API, the only difference is the base URL.

URI nhnApiUri = URI.create("https://api.nhn.digipost.no");
DigipostClientConfig config = DigipostClientConfig.newConfiguration().digipostApiUri(nhnApiUri).build();

Send messages

The Java client library also contains some example code which include similar examples.

Send a message to a recipient

To send a message to a recipient in Digipost, you need to choose a way to identify the recipient, instantiate a primary Document and the containing Message. Finally these are given to the client as well as the content of the document as an InputStream. The actual API communication will happen when you invoke the .send() method.

Send using a personal identification number for the recipient

PersonalIdentificationNumber pin = new PersonalIdentificationNumber("26079833787");
UUID documentUuid = UUID.randomUUID();
Document primaryDocument = new Document(documentUuid, "Document subject", FileType.PDF);

Message message = Message.newMessage("messageId", primaryDocument)
        .recipient(pin)
        .build();

client.createMessage(message)
        .addContent(primaryDocument, Files.newInputStream(Paths.get("content.pdf")))
        .send();

Other recipient types

There are other recipient types available to identify recipients of messages. Note that some recipient types may require special permissions to be set up in order to be used. E.g. bank account number requires such permissions, and are not enabled by default.

NameAndAddress nameAndAddress = new NameAndAddress("Ola Nordmann", "Gateveien 1", "Oppgang B", "0001", "Oslo");
BankAccountNumber accountNum = new BankAccountNumber("12345123451");

Multiple documents in one message

A message is required to have at least one document, the primary document. Additional documents can also be included as attachments.

PersonalIdentificationNumber pin = new PersonalIdentificationNumber("26079833787");

Document primaryDocument = new Document(UUID1, "Document subject", FileType.PDF);

Document attachment1 = new Document(UUID2, "Attachment1 subject", FileType.PDF);
Document attachment2 = new Document(UUID3, "Attachment2 subject", FileType.PDF);

Message message = Message.newMessage("messageId", primaryDocument)
        .recipient(pin)
        .attachments(attachment1, attachment2)
        .build();

client.createMessage(message)
        .addContent(primaryDocument, Files.newInputStream(Paths.get("main_document_content.pdf")))
        .addContent(attachment1, Files.newInputStream(Paths.get("attachment1_content.pdf")))
        .addContent(attachment2, Files.newInputStream(Paths.get("attachment2_content.pdf")))
        .send();

Send invoice

PersonalIdentificationNumber pin = new PersonalIdentificationNumber("26079833787");

// An invoice requires four extra fields (KID, amount, account and due date). The use
// of the Invoice class will trigger payment functionality i Digipost.
Invoice invoice = new Invoice(UUID1, "Invoice subject", FileType.PDF, null, null, null,
                              AuthenticationLevel.PASSWORD, SensitivityLevel.NORMAL, "704279604",
                              new BigDecimal("1.20"), "82760100435", LocalDate.of(2015, 5, 5));

Message message = Message.newMessage("messageId", invoice)
        .recipient(pin)
        .build();

client.createMessage(message)
        .addContent(invoice, Files.newInputStream(Paths.get("invoice.pdf")))
        .send();

Send a message with SMS notification

PersonalIdentificationNumber pin = new PersonalIdentificationNumber("26079833787");

// The time the SMS is sent out can be based on time after letter is delivered
// or a specific date. This example specifies that the SMS should be sent out
// one day after the letter i delivered.
Document primaryDocument = new Document(UUID1, "Document subject", FileType.PDF, null,
                                        new SmsNotification(1), null,
                                        AuthenticationLevel.PASSWORD, SensitivityLevel.NORMAL);

Message message = Message.newMessage(UUID2, primaryDocument)
        .recipient(pin)
        .build();

client.createMessage(message)
        .addContent(primaryDocument, Files.newInputStream(Paths.get("content.pdf")))
        .send();

Send letter with fallback to print

PersonalIdentificationNumber pin = new PersonalIdentificationNumber("26079833787");

Document primaryDocument = new Document(UUID1, "Document subject", FileType.PDF);

PrintDetails printDetails = new PrintDetails(
        new PrintRecipient("Ola Nordmann", new NorwegianAddress("Prinsensveien 123", "0460", "Oslo")),
        new PrintRecipient("Norgesbedriften", new NorwegianAddress("Akers Àle 2", "0400", "Oslo")),
        PrintDetails.PrintColors.MONOCHROME, PrintDetails.NondeliverableHandling.RETURN_TO_SENDER);

Message message = Message.newMessage(UUID2, primaryDocument)
        .recipient(new MessageRecipient(pin, printDetails))
        .build();

// addContent can also take a third parameter which is the file/ipnput stream that will be used only
// for physical mail. The below example uses the same file/input stream in both channels (digital and physical mail)
MessageDelivery result = client.createMessage(message)
        .addContent(primaryDocument, Files.newInputStream(Paths.get("content.pdf")))
        .send();

Send letter with html

If you want to be able to send HTML-documents you first need to contact Digipost to activate the feature for your broker/sender. Then it is just matter of specifing HTML as the filetype and serve an html-file as content. Bevare that there are strict rules to what is allowed. These rules are quite verbose. But we have open sourced the html validator and santizer software we use to make sure that html conforms to these rules. Check out https://github.com/digipost/digipost-html-validator. If you preencrypt your document, this validation will be performed in the client instead of the server so that you can be confident that you recipient will be able to open the document.

PersonalIdentificationNumber pin = new PersonalIdentificationNumber("26079833787");
UUID documentUuid = UUID.randomUUID();
Document primaryDocument = new Document(documentUuid, "Document subject", FileType.HTML);

Message message = Message.newMessage("messageId", primaryDocument)
        .recipient(pin)
        .build();

client.createMessage(message)
        .addContent(primaryDocument, Files.newInputStream(Paths.get("content.html")))
        .send();

Send letter with higher security level

PersonalIdentificationNumber pin = new PersonalIdentificationNumber("26079833787");

// TWO_FACTOR - require BankID or BuyPass authentication to open letter
// SENSITIVE - Sender information and subject will be hidden until Digipost user
// is logged in at the appropriate authentication level
Document primaryDocument = new Document(UUID1, "Document subject", FileType.PDF, null, null, null,
                                        AuthenticationLevel.TWO_FACTOR, SensitivityLevel.SENSITIVE);

Message message = Message.newMessage(UUID2, primaryDocument)
        .recipient(pin)
        .build();

client.createMessage(message)
        .addContent(primaryDocument, Files.newInputStream("content.pdf")))
        .send();

Send a message with extra computer readable data

With version 7 of the Digipost API, messages can have extra bits of computer readable information that allows the creation of a customized, dynamic user experience for messages in Digipost. These extra bits of information are referred to as instances of “Datatypes”.

All datatypes are sent in the same way. Each document can accommodate one datatype-object. An exhaustive list of available datatypes and their documentation can be found at digipost/digipost-data-types.

For convenience, all datatypes are available as java-classes in the java client library.

Datatype Appointment

In this example, an appointment-datatype that allows for certain calendar-related functions is added to a message.

PersonalIdentificationNumber pin = new PersonalIdentificationNumber("26079833787");
UUID messageUUID = UUID.randomUUID();

ZonedDateTime startTime = ZonedDateTime.of(2017, 10, 23, 10, 0, 0, 0, ZoneId.systemDefault());
AppointmentAddress address = new AppointmentAddress("Storgata 1", "0001", "Oslo");
Info preparation = new Info("Preparation", "Please do not eat or drink 6 hours prior to examination");
Info about = new Info("About Oslo X-Ray center", "Oslo X-Ray center is specialized in advanced image diagnostics...");
List<Info> info = Arrays.asList(preparation, about);

Appointment appointment = new Appointment(
        startTime, startTime.plusMinutes(30), "Please arrive 15 minutes early",
        "Oslo X-Ray center", address, "Lower back examination", info, Language.EN);

Document primaryDocument = new Document(messageUUID, "X-Ray appointment", FileType.PDF, appointment);

Message message = Message.newMessage("messageId", primaryDocument)
        .recipient(pin)
        .build();

client.createMessage(message)
        .addContent(primaryDocument, Files.newInputStream(Paths.get("content.pdf")))
        .send();

This Datatype enhances a message in Digipost with a button which sends the user to an external site. The button can optionally have a deadline, a description and a custom text.

PersonalIdentificationNumber pin = new PersonalIdentificationNumber("26079833787");
UUID messageUUID = UUID.randomUUID();

URI externalLinkTarget = URI.create("https://example.org/loan-offer/uniqueCustomerId/");
ZonedDateTime deadline = ZonedDateTime.of(2018, 10, 23, 10, 0, 0, 0, ZoneId.systemDefault());

ExternalLink externalLink = new ExternalLink(externalLinkTarget, deadline,
        "Please read the terms, and use the button above to accept them. The offer expires at 23/10-2018 10:00.",
        "Accept offer");

Document primaryDocument = new Document(messageUUID, "Housing loan application", FileType.PDF, externalLink);

Message message = Message.newMessage("messageId", primaryDocument)
        .recipient(pin)
        .build();

client.createMessage(message)
        .addContent(primaryDocument, Files.newInputStream("terms.pdf")))
        .send();

Identify user based on personal identification number

PersonalIdentificationNumber pin = new PersonalIdentificationNumber("26079833787");

Identification identification = new Identification(pin);

IdentificationResult identificationResult = client.identifyRecipient(identification);

Create Digipost User Accounts

Create new or activate existing Digipost user account.

SenderId sender = SenderId.of(123456);
UserInformation user = new UserInformation(
  new NationalIdentityNumber("01013300001"),
  new PhoneNumber("+4799998888"),
  new EmailAddress("user@example.com")
);

UserAccount userAccount = client.createOrActivateUserAccount(sender, user);

DigipostAddress digipostAddress = userAccount.getDigipostAddress();
EncryptionKey encryptionKey = userAccount.getEncryptionKey();

Receive messages

The inbox API makes it possible for an organisation to manage messages received in Digipost.

Get documents in inbox

The inbox call outputs a list of documents ordered by delivery time. Offset is the start index of the list, and limit is the max number of documents to be returned. The offset and limit is therefore not in any way connected to InboxDocument.id.

The values offset and limit is meant for pagination so that one can fetch 100 and then the next 100.

//get first 100 documents
Inbox first100 = client.getInbox(SenderId.of(123456), 0, 100);

//get next 100 documents
Inbox next100 = client.getInbox(SenderId.of(123456), 100, 100);

We have now fetched the 200 newest inbox documents. As long as no new documents are received, the two API-calls shown above will always return the same result. If we now receive a new document, this will change. The first 100 will now contain 1 new document and 99 documents we have seen before. This means that as soon as you stumble upon a document you have seen before you can stop processing, given that all the following older ones have been processed.

Download document content

Inbox inbox = client.getInbox(SenderId.of(123456));

InboxDocument documentMetadata = inbox.documents.get(0);

System.out.println("Content type is: " + documentMetadata.getContentType());
InputStream documentContent = client.getInboxDocumentContent(documentMetadata);

Delete document

Inbox inbox = client.getInbox(SenderId.of(123456));

InboxDocument documentMetadata = inbox.documents.get(0);

client.deleteInboxDocument(documentMetadata);

Download attachment content

Inbox inbox = client.getInbox(SenderId.of(123456));

InboxDocument documentMetadata = inbox.documents.get(0);
InboxDocument attachment = documentMetadata.getAttachments().get(0);

System.out.println("Content type is: " + attachment.getContentType());
InputStream attachmentContent = client.getInboxDocumentContent(attachment);

Archive functionality

The archive API makes it possible for an organisation to manage documents in archives. These files are kept in separate archives, and the files belong to the sender organisation.

Archive documents to an archive

Let’s say you want to archive two documents eg. an invoice and an attachment and you want to have some kind of reference to both documents. You can do that by describing the two documents with ArchiveDocument. Then you need to create an archive and add the documents to the archive. In the following example we use a default archive. You then need to send this archive and attach the actual files to the request by linking the ArchiveDocument with a file and send.

// 1. We describe the documents
final ArchiveDocument invoice = new ArchiveDocument(
    UUID.randomUUID()
    , "invoice_123123.pdf"
    , "pdf"
    , "application/pdf"
);
final ArchiveDocument attachment = new ArchiveDocument(
    UUID.randomUUID()
    , "attachment_123123.pdf"
    , "pdf"
    , "application/pdf"
);

// 2. We create an archive and add the documents to it
Archive archive = Archive.defaultArchive()
    .documents(invoice, attachment)
    .build();

// 3. We create a request to archive the files with reference between the ArchiveDocument and the actual file
client.archiveDocuments(archive)
    .addFile(invoice, readFileFromDisk("invoice_123123.pdf"))
    .addFile(attachment, readFileFromDisk("attachment_123123.pdf"))
    .send();

Get a list of archives

An organisation can have many archives, or just the default unnamed archive. That is up to your design wishes. To get a list of the archives for a given Sender, you can do this:

//get a list of the archives
Archives archives = client.getArchives(SenderId.of(123456));

The class Archives holds a list of Archive where you can see the name of the archive.

Iterate documents in an archive

You can get content of an archive with paged requests. Under is an example of how to iterate an archive. However it’s use is strongly discouraged because it leads to the idea that an archive can be itereated. We expect an archive to possibly reach many million rows so the iteration will possibly give huge loads. On the other hand being able to dump all data is a nessary feature of any archive.

Please use fetch document by UUID or referenceID instead to create functionality on top of the archive. You should on your side know where and how to get a document from an archive. You do this by knowing where you put a file you want to retrieve.

final Archives archives = client.getArchives();

Archive current = archives.getArchives().get(0);
final List<ArchiveDocument> documents = new ArrayList<>();

while (current.getNextDocuments().isPresent()) {
current = current.getNextDocuments()
.map(client::getArchiveDocuments)
.orElse(new Archive());

documents.addAll(current.getDocuments());
}

// This prints to total content of the list of documents
System.out.println(documents);

Get documents by referenceID

You can retrieve a set of documents by a given referenceID. You will then get the documents listed in their respective archives in return.

final Archives archives = client.getArchiveDocumentsByReferenceId("REFERENCE_ID");

Get documents by uuid

You can retrieve a set of documents by the UUID that you give the document when you achive it. In the example above we use UUID.randomUUID() to generate an uuid. You can either store that random uuid inyour database for retrieval later, or you can generate a deterministic uuid based on your convensions for later retrieval.

You will get in return an instance of Archive which contains information on the archive the document is contained in and the actual document. From this you can fetch the actual document.

final UUID myConvensionUUID = UUID.fromString("vedlegg:123123:txt");

final Archive archiveWithDocument = client.getArchiveDocumentByUuid(myConvensionUUID);

You can get the actual content of a document after you have retrieved the archive document. Below is an example of how you can achieve this with a given ArchiveDocument. In the resulting ArchiveDocumentContent, you will get a url to the content which expires after 30 seconds.

// This ArchiveDocument must be retrieved beforehand using one of the methods described above
final ArchiveDocument archiveDocument;

URI getDocumentContentURI = archiveDocument.getDocumentContent().orElseThrow();
ArchiveDocumentContent content = client.getArchiveDocumentContent(getDocumentContentURI);

Get content of a document as a stream

In addition to a single-use link, you also have the option to retrieve the content of a document directly as a byte stream.

// This ArchiveDocument must be retrieved beforehand using one of the methods described above
final ArchiveDocument archiveDocument;

URI getDocumentContentStreamURI = archiveDocument.getDocumentContentStream().orElseThrow();
InputStream content = client.getArchiveDocumentContentStream(getDocumentContentStreamURI);

Using archive as a broker

It is possible to be a broker for an actual sender. Most of the api described above also support the use of SenderId to specify who you are archiving for.

eg.:

client.getArchives(SenderId.of(123456))
client.getArchiveDocumentsByReferenceId(SenderId.of(123456), "REFERENCE_ID");
client.getArchiveDocumentByUuid(SenderId.of(123456), myConvensionUUID);


Archive archive = Archive.defaultArchive()
                .documents(faktura)
                .senderId(SenderId.of(123456))
                .build();