Skip to content

Examples

All examples perform the same procedure for one PDF document (document.pdf in the working folder), following the order of the steps in the integration guide:

Step of the guide Route in the example
1. Open a transaction in ePotpis /sign
2. Prepare the document (PreSign) /sign
3. Hand the hash to ePotpis /sign
4. Send the user to ePotpis /sign
5. Receive the signed data /esign/signed
6. Embed the signature (PostSign) /esign/signed
7. The result /done and /failed

The signature is embedded at level LTA with a time stamp, and the signed document is saved as <verification code>.pdf.

The signature is invisible (addVisualData false). For a signature with a visual, also send the signer’s data (personNaturalData) in PreSign, as described in the guide.

The examples read the values shown on the Integration tab of your service’s page from environment variables:

Variable Value
ESIGN_SYSTEM_ID System ID.
ESIGN_SERVICE_ID Service ID.
ESIGN_DOCUMENT_ID The number of the kind of document (documentId), before its name.
ADES_KEY The signing API key (Show button). Read it from your environment’s secrets, never from code.
TSA_USER, TSA_PASSWORD The user name and password of your time stamp service, for level LTA.

The example runs on port 3200. ePotpis sends the signed data from its own server; the example’s address must therefore be HTTPS and reachable from the internet. During development, expose the local port through a tunnel, for example the free Cloudflare Quick Tunnel, which assigns a temporary address https://….trycloudflare.com:

Prozor terminala
cloudflared tunnel --url http://127.0.0.1:3200

Then enter the following addresses, using the tunnel address, in the service’s settings (Settings tab):

Setting Value
Base URL https://<tunnel address>
Sign PDF URL https://<tunnel address>/esign/signed
Success URL https://<tunnel address>/done?token=
Error URL https://<tunnel address>/failed?token=

Start the example and open http://127.0.0.1:3200/sign in the browser.

Java 25, Spring Boot 4.1.2 (spring-boot-starter-webmvc, which includes RestClient) and Maven.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.1.2</version>
</parent>
<groupId>com.example</groupId>
<artifactId>certilia-signing</artifactId>
<version>1.0.0</version>
<properties>
<java.version>25</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
src/main/resources/application.properties
server.port=3200
src/main/java/com/example/CertiliaSigning.java
package com.example;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClient;
@SpringBootApplication
@RestController
public class CertiliaSigning {
static final String EPOTPIS = "https://esign.test.certilia.com/api/v2/epotpis";
static final String PADES = "https://api.test.certilia.com/pades/v2";
static final Map<String, Object> RECEIVED = Map.of("signingDataResponse", Map.of("status", "201"));
final RestClient http = RestClient.create();
final Map<String, Transaction> transactions = new ConcurrentHashMap<>();
static final class Transaction {
final String verificationCode;
final String hash;
volatile boolean signed;
Transaction(String verificationCode, String hash) {
this.verificationCode = verificationCode;
this.hash = hash;
}
}
public static void main(String[] args) {
SpringApplication.run(CertiliaSigning.class, args);
}
Map<?, ?> call(HttpMethod method, String url, Object body, boolean ades) {
Map<?, ?> answer = http.method(method).uri(url)
.contentType(MediaType.APPLICATION_JSON)
.headers(headers -> { if (ades) headers.setBearerAuth(System.getenv("ADES_KEY")); })
.body(body)
.retrieve()
.body(Map.class);
if (answer == null || answer.get("error") != null) throw new IllegalStateException(url + ": " + answer);
return answer;
}
static Map<?, ?> first(Map<?, ?> map, String key) {
return (Map<?, ?>) ((List<?>) map.get(key)).get(0);
}
// Step 1: open a transaction in ePotpis.
// Step 2: prepare the document (PreSign).
// Step 3: hand the hash to ePotpis.
// Step 4: send the user to ePotpis.
@GetMapping("/sign")
ResponseEntity<Void> sign() throws Exception {
Map<?, ?> opened = (Map<?, ?>) call(HttpMethod.POST, EPOTPIS, Map.of(
"authData", Map.of(
"systemId", System.getenv("ESIGN_SYSTEM_ID"),
"serviceId", System.getenv("ESIGN_SERVICE_ID"),
"documentId", System.getenv("ESIGN_DOCUMENT_ID")),
"signingData", Map.of("numOfDocs", "1", "mimetype", "pdf", "signatureType", "sign")), false).get("authDataResponse");
String token = (String) opened.get("token");
String verificationCode = (String) ((List<?>) opened.get("verificationCodes")).get(0);
String document = Base64.getEncoder().encodeToString(Files.readAllBytes(Path.of("document.pdf")));
Map<?, ?> prepared = call(HttpMethod.POST, PADES, Map.of(
"token", token,
"signatureFormat", "pades",
"signatureType", "sign",
"addVisualData", false,
"documents", List.of(Map.of("verificationCode", verificationCode, "base64Document", document, "documentName", "document.pdf"))), true);
String hash = (String) first(prepared, "documents").get("hash");
transactions.put(token, new Transaction(verificationCode, hash));
Map<?, ?> submitted = call(HttpMethod.PATCH, EPOTPIS + "/hash", Map.of(
"authData", Map.of("token", token),
"signingData", Map.of("documents", List.of(Map.of("hash", hash, "verificationCode", verificationCode)))), false);
return ResponseEntity.status(302).location(URI.create((String) submitted.get("redirectURL"))).build();
}
// Step 5: ePotpis sends the signed data.
// Step 6: the AdES web API embeds the signature (PostSign).
@PostMapping("/esign/signed")
ResponseEntity<Map<String, Object>> signed(@RequestBody Map<String, Object> body) {
if (body.get("error") instanceof Map<?, ?> refused) {
// The user refused to sign.
transactions.remove(String.valueOf(refused.get("token")));
return ResponseEntity.status(201).body(RECEIVED);
}
String token = String.valueOf(((Map<?, ?>) body.getOrDefault("authData", Map.of())).get("token"));
Map<?, ?> signingData = (Map<?, ?>) body.getOrDefault("signingData", Map.of());
Transaction transaction = transactions.get(token);
Map<?, ?> signed = signingData.get("documents") == null ? Map.of() : first(signingData, "documents");
if (transaction == null || !transaction.verificationCode.equals(signed.get("verificationCode"))) {
return ResponseEntity.status(400).body(Map.of("error", Map.of("status", "400", "code", "UNKNOWN_TRANSACTION", "title", "Unknown transaction")));
}
try {
Map<?, ?> result = call(HttpMethod.PATCH, PADES, Map.of(
"token", token,
"signatureFormat", "pades",
"signatureLevel", "lta",
"userCertificate", signingData.get("userCertificate"),
"tsaAccess", Map.of(
"url", "https://tsa.test.certilia.com/qts",
"username", System.getenv("TSA_USER"),
"password", System.getenv("TSA_PASSWORD")),
"documents", List.of(Map.of(
"verificationCode", transaction.verificationCode,
"hash", transaction.hash,
"signedHash", signed.get("signedHash")))), true);
String document = (String) first(result, "documents").get("base64Document");
Files.write(Path.of(transaction.verificationCode + ".pdf"), Base64.getDecoder().decode(document));
transaction.signed = true;
return ResponseEntity.status(201).body(RECEIVED);
} catch (Exception error) {
return ResponseEntity.status(400).body(Map.of("error", Map.of("status", "400", "code", "POSTSIGN_FAILED", "title", String.valueOf(error.getMessage()))));
}
}
// Step 7: ePotpis sends the user back with the transaction's token.
@GetMapping("/done")
String done(@RequestParam(required = false) String token) {
Transaction transaction = token == null ? null : transactions.get(token);
return transaction != null && transaction.signed ? "The document is signed: " + transaction.verificationCode + ".pdf" : "The signature is not embedded yet.";
}
@GetMapping("/failed")
String failed() {
return "The signing failed.";
}
}
Prozor terminala
mvn package
java -jar target/certilia-signing-1.0.0.jar