API Authentication

Prima's REST API uses OAuth 2.0. Every request to the API must carry a JSON Web Token (JWT) obtained from your Prima server's token endpoint. This page covers setting up an API client, requesting and refreshing a token, and best practices for using it in an integration.

Note: This page covers authenticating API clients that read or write lab data. To configure how lab staff sign in to Prima itself (LDAP, Okta, Windows), see Server Configuration → Authentication.

Setting up an API client

API clients are created and managed from your internal Prima website (typically something like 'https://prima.your-workplace-website.com'), under Admin → API Clients. Each client represents one integration and gets its own credentials, scopes, and token settings.

  1. Go to Admin → API Clients and create a new client.
  2. Give it a Client Name (for your own reference) and a Client ID.
  3. Save the client, then add a Client Secret from the client's edit page. The secret is shown once — store it somewhere secure.
  4. Choose the Grant Type(s) the client is allowed to use (see below).
  5. Choose the Scopes the client needs: read-only, or read/write.
  6. If the client will write data (create or modify cases, specimens, etc.), assign it a Service User. Prima attributes the records it creates or edits to this user, which keeps your audit trail meaningful. Read-only clients can leave this blank.

Refresh tokens

If an integration needs to stay authenticated over a long period without holding onto the client secret for every renewal, enable refresh tokens on the client:

Choosing a grant type

Grant type Use for
client_credentials Recommended for server-to-server integrations. The client authenticates with its own ID and secret — no end-user login is involved.
refresh_token Exchanging a refresh token for a new access token without repeating the original grant.
password Resource owner password grant, tied to a specific Prima user login. Supported for backward compatibility; prefer client_credentials for new integrations.
authorization_code Interactive applications where a person logs in and grants consent (e.g. a browser-based tool). Requires a Redirect URI configured on the client.

Requesting a token

Send a POST to your Prima server's token endpoint with the client's credentials:

POST https://your-prima-server/connect/token
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials
client_id={ClientId}
client_secret={ClientSecret}
scope=api_read_all api_write_all

A successful response looks like:

{
  "access_token": "eyJhbGciOiJSUzI1NiIs...",
  "expires_in": 3600,
  "token_type": "Bearer"
}

Include offline_access in the requested scope (and enable refresh tokens on the client) to also receive a refresh_token in the response.

Refreshing a token

Before the access token expires, exchange the refresh token for a new one rather than repeating the original grant:

POST https://your-prima-server/connect/token
Content-Type: application/x-www-form-urlencoded

grant_type=refresh_token
client_id={ClientId}
client_secret={ClientSecret}
refresh_token={RefreshToken}
Warning: If the client's Refresh Token Usage is set to One-Time Only, the response contains a new refresh_token that replaces the one just used. Save it — the old value stops working.

Using the access token

Include the access token on every API request as a bearer token:

GET https://your-prima-server/api/v2/Case
Authorization: Bearer eyJhbGciOiJSUzI1NiIs...

Access tokens are short-lived (one hour by default). Cache the token and reuse it for the duration of its lifetime instead of requesting a new one before every call — request a new one once it's close to expiring, or use the refresh token flow if it's configured.

Best practices

Troubleshooting

401 Unauthorized when requesting a token
Double check the client_id and client_secret, and confirm the grant type being used is allowed on the client.
401 Unauthorized calling the API with a token
The token may have expired, or the requested scope may not include what the endpoint requires.
403 Forbidden on write requests
The client's scopes may be missing write access, or the client doesn't have a Service User assigned.
Refresh token no longer works
Refresh tokens expire (30 days by default) and, if One-Time Only usage is configured, are replaced with a new value every time they're used — reusing an old one fails.

Example clients

An example .NET client is available at github.com/Fortelinea/PrimaRESTExampleClient. It demonstrates the resource owner password grant used by older (V1) integrations; new integrations should follow the client_credentials pattern described above instead.