# API connections How to connect your own program to the account with OAuth: Client ID and secret, a new secret, disconnecting and how long tokens last. An API connection lets your own program work with your company's data, e.g. fetch orders or change their statuses. The program works on the account of the user who connected it: it has the same permissions, and its changes show in the history under that user's name, with the **API** source. > **Good to know:** You connect the program to an existing account: you log in with your e-mail and password and approve the access. You cannot create a new account this way. > **Good to know:** For security, connect the program through a separate user, e.g. "API user", not through an administrator account. ## Where to find it Click the gear icon at the bottom of the left menu and choose **API connections**. The list shows all connections of the company: name, Client ID, redirect address, who created it and who is connected through it. The counter under the heading shows how many connections you already have - a company can have at most 20. ![The gear icon menu with API connections](https://ritevo.com/docs/general/en-api-connections-menu.webp) ![The company list of API connections with the name, return address and connected users](https://ritevo.com/docs/general/en-api-connections-list.webp) > **Good to know:** Every user of the company sees the list, but only an **Administrator** can create connections, generate a new secret and delete them. ## Creating an API connection Each connection gets a Client ID and a secret. You put them in the program's settings. The Client ID and secret alone do not open the account - the program gets access only when a user logs in and approves it. 1. The administrator clicks **Create API connection** in the top right corner. 2. They enter a name and a redirect address. The redirect address is the page of the program the user comes back to after approving the access. 3. They click **Create** and copy the **Client ID** and **Client secret** into the program's settings. 4. The program opens the login page for the user. The user logs in and clicks **Allow**, then comes back to the redirect address with a one-time code. 5. The program exchanges the code for tokens and calls the API with the access token. ![The create API connection window with the name and return address](https://ritevo.com/docs/general/en-api-connections-create.webp) ![The Client ID and Client secret of a new API connection, shown only once](https://ritevo.com/docs/general/en-api-connections-credentials.webp) - The redirect address must start with https and be reachable from the internet. http and localhost addresses are rejected. - You see the secret only once, right after creating the connection. Later it cannot be read - you can only generate a new one. - The connection name must not contain the word "Ritevo". - Only users of your company can connect through it, each with their own account. - The consent screen shows on every connection, also when the program was approved before. You always see which account the program gets access to. - Send the access token in the `Authorization: Bearer` header to `https://api.ritevo.com`. - Deleting a connection cuts off everyone who connected through it. Authorization: the browser goes to `GET https://oauth.ritevo.com/oauth2/authorize` with `response_type=code`, `client_id`, `redirect_uri` (exactly as registered), `code_challenge`, `code_challenge_method=S256`, `state`. Code exchange: `POST https://oauth.ritevo.com/oauth2/token` with `grant_type=authorization_code`, `code`, `redirect_uri`, `code_verifier` and client authentication - `client_secret_basic` (Basic header) or `client_secret_post` (`client_id` and `client_secret` in the body). Response: `access_token` (JWT), `refresh_token`, `expires_in`. API calls: header `Authorization: Bearer ` on `https://api.ritevo.com`. ## A new secret - If the secret is lost, the administrator generates a new one with the key icon next to the connection. - The old secret stops working at once. - Tokens the program already has keep working for at most an hour. After that the program refreshes the token with the new secret. ## Disconnecting - Every connected user has the **Disconnect** icon. Anyone can disconnect themselves, the administrator can disconnect anyone. - A disconnected user loses access through the connection, including the tokens the program already has. To use the program again, they connect once more. - The other users stay connected. - A deactivated user is disconnected from all connections. ## How long a connection lasts - The code from the login works once and only for a minute. - The access token works for an hour. - The refresh token works for 30 days and only once. Every refresh gives the program a new pair of tokens. If the program does not refresh for 30 days, it has to be connected again. - Protect the refresh token like a password. If someone takes it and uses it before your program does, the program gets an `invalid_grant` error on its next refresh. Then disconnect yourself with the **Disconnect** icon or ask the administrator to do it - this also cuts off whoever took the token. After that, connect the program again. ## Change history Changes made by the program show in the history under the name of the user who connected it, with the **API** source. This way you can tell the program's changes from your own. **Audits** keep, with the connection name, creating a connection, a new secret and deleting it, as well as every connecting and disconnecting of a program. Failed attempts are kept too, e.g. a refusal on the consent screen, a wrong secret or connecting a deactivated account. An attempt by someone outside the company is kept without their name. The change source (`eventSource`) for OAuth tokens is `USER_API`, for work in the app `USER`. Audit actions (`GET /company/audit-log`, field `action`): `OAUTH_APP_CREATED`, `OAUTH_APP_SECRET_REGENERATED`, `OAUTH_APP_DELETED`, `OAUTH_APP_CONNECTED` (code exchanged for tokens - the program got access; `eventSource` `USER_API`, `ipAddress` of the program's server), `OAUTH_APP_CONNECT_DENIED` (refused on the consent screen), `OAUTH_APP_CONNECT_USER_INACTIVE` (connecting a deactivated account), `OAUTH_APP_CONNECT_OTHER_COMPANY` (a user of another company tried to approve, no `userId`), `OAUTH_APP_CONNECT_FAILED` (other failed login or code exchange, e.g. an expired or used code, wrong PKCE or return address), `OAUTH_APP_AUTH_FAILED` (wrong secret, no user), `OAUTH_APP_REFRESH_USER_INACTIVE` (a new token for a deactivated account), `OAUTH_APP_REFRESH_FAILED` (other refused refresh, e.g. an expired or used refresh token), `OAUTH_APP_DISCONNECTED`. `description` holds the connection name, and when another user was disconnected, their name too. Successful token refreshes are not audited. ## API `GET /company/oauth-apps` (any user; list: `clientId`, `name`, `redirectUris`, `createdByUserId`, `connectedUserIds` - users of the company with a working token, ids from `GET /company/users`, `createdAt`), `POST /company/oauth-apps` (ADMIN only; body `name` up to 100 characters, without the word "Ritevo", `redirectUris` 1-10 https addresses; returns `clientId` and `clientSecret` once), `POST /company/oauth-apps/{clientId}/secret` (ADMIN only; new secret, the old one stops working), `DELETE /company/oauth-apps/{clientId}/connections/{userId}` (disconnects a user; anyone can disconnect themselves, others only an ADMIN), `DELETE /company/oauth-apps/{clientId}` (ADMIN only). Errors are a ProblemDetail with a `code`: `OAUTH_APP_NOT_FOUND` (404), `USER_NOT_FOUND` (404), `INVALID_REDIRECT_URI` (400), `FORBIDDEN_OPERATION` (403, not an administrator), `LIMIT_EXCEPTION` (422, over 20 connections in the company). Current paths and fields are in Swagger: https://api.ritevo.com/api/v1/api-docs. --- All help in one file: https://ritevo.com/llms-full.txt. Help map with links to every page: https://ritevo.com/llms.txt.