# Integrate the onetime.ie API v1

Implement an integration that creates encrypted, one-time links for UTF-8 text and can optionally read the sender's link status. Work within the host project's conventions. Add mock-based tests and explain how to run with an operator-issued API key.

API origin: https://onetime.ie/
API reference: https://onetime.ie/api-docs
Link format: https://onetime.ie/developers/link-format
Web Crypto module: https://onetime.ie/assets/crypto.mjs

## Credentials and data flow

- Use a trusted integration runtime, such as Node.js 20+. Read `ONETIME_API_KEY` from the environment or a secrets manager. Do not expose it in a browser bundle or a repository.
- Send `Authorization: Bearer <key>` and `Content-Type: application/json` over HTTPS. Disable redirects when sending credentials. A create key needs `secrets:create`. A status key needs `status:read` for the same integration.
- Use the Web Crypto module as a local dependency. Review and bundle it with your application. Keep the plaintext, passphrase, encryption key and raw release token on the trusted client during creation.
- Keep credentials, complete URLs, fragments and status tokens out of logs, analytics and error reports. Return links only to their intended holders. Send an optional passphrase over a separate channel.

## Create a link

1. Call `prepareSecret(text, ttlSeconds)`. Text must be 1–16,384 UTF-8 bytes. The allowed TTL values are `900`, `3600`, `86400` and `604800` seconds.
2. Send exactly `secret.request` as JSON to `POST /api/v1/secrets`. Its fields are `version` (integer 1), `token_hash` (lowercase hex SHA-256 of the raw 32-byte release token), `nonce`, `ciphertext` and `ttl_seconds`. The nonce and ciphertext are unpadded base64url. The body limit is 24 KiB.
3. Check for HTTP `201`. The response contains `id`, `expires_at`, `visit_token` and `status_token`. Build the links locally using the module, keeping the fragments after the `#`. Share the recipient URL. Keep the status URL private.

```js
import { prepareSecret, revealEnvelope, statusEnvelope } from './crypto.mjs';

const origin = new URL('https://onetime.ie/');
const apiKey = process.env.ONETIME_API_KEY;
if (!apiKey) throw new Error('Set ONETIME_API_KEY');

export async function createLink(text, ttlSeconds = 3600, passphrase = '') {
    const secret = await prepareSecret(text, ttlSeconds);
    const response = await fetch(new URL('/api/v1/secrets', origin), {
        method: 'POST',
        redirect: 'error',
        headers: {
            Authorization: 'Bearer ' + apiKey,
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(secret.request),
    });
    if (response.status !== 201) {
        throw new Error('Link creation failed (HTTP ' + response.status + ')');
    }

    const { id, expires_at, visit_token, status_token } = await response.json();
    const recipient = await revealEnvelope(id, visit_token, secret, passphrase);
    return {
        recipientUrl: new URL('/reveal#' + recipient, origin).href,
        statusUrl: new URL('/status#' + statusEnvelope(id, status_token), origin).href,
        expiresAt: expires_at,
    };
}
```

## Read status and handle errors

Send `{ "id": "...", "token": "..." }` to `POST /api/v1/secrets/status` with the status scope. The value of `token` is the returned `status_token`. If starting with a private URL, call `parseStatus(url.hash.slice(1))` for the request body. The body limit is 1 KiB.

HTTP `200` returns `state`, `created_at`, `expires_at`, `opened_at` and `revealed_at`. The states are `Waiting`, `Opened`, `Revealed` and `Expired`. An opening can come from an automated visitor. A reveal means the server released ciphertext. Neither timestamp verifies who read the text. Status expires 24 hours after link expiry.

Errors have an `error` field. Treat `401` as missing, invalid or revoked credentials, `403` as denied scope or origin, `404` as unavailable status, and `413` or `422` as invalid payload. On `429`, respect `Retry-After`. A timed-out create may have succeeded. A new create request makes a separate link.

Opening or previewing the recipient page does not use the link. Leave reveal to the recipient in the browser. Release consumes the record once, so avoid automatic reveal, polling for plaintext and retries after an uncertain reveal.

## Verify the integration

Mock the API for routine tests. Assert that the create request contains ciphertext and the token hash but never plaintext, the passphrase, the key or the raw release token. Check both generated URLs, status token handling, non-success responses and redaction. Use fictional data for a live smoke test only when a user supplies an authorised key and asks to run it.
