How to send emails with Java / Spring

Send emails from Spring with `RestClient` (Spring 6+), calling the Publiq REST API.

There is no official SDK — call the Publiq REST API with your language HTTP client, from the server, with the API key in the Authorization header.

import org.springframework.web.client.RestClient;
import java.util.Map;

public class PubliqService {
  private final RestClient client = RestClient.create();

  public void sendWelcome(String to, String name) {
    client.post()
      .uri("https://api.publiq.digital/v1/emails")
      .header("Authorization", "Bearer " + System.getenv("PUBLIQ_API_KEY"))
      .body(Map.of(
        "from", "you@yourdomain.com",
        "to", to,
        "templateKey", "welcome-email",
        "variables", Map.of("first_name", name)
      ))
      .retrieve()
      .toBodilessEntity();
  }
}

Best practices

  • Always call Publiq from the server — the API key goes in the Authorization header, never on the client.
  • On 429/5xx, retry with exponential backoff (respect Retry-After).
  • Prefer templateKey over inline HTML to keep content versioned.
  • Handle errors via the response error.code field — see Errors.
  • Send from a verified domain — see Domains.

See also

How to send emails with Java / Spring — Publiq Docs