# API keys integration An API key lets your own systems access your Adfin account. For example, use an API key to connect your ERP, a script or a data warehouse to Adfin. Each API key belongs to one Adfin account. A key can access only the data of that account, and only with the permissions that you give it. Prerequisites - You have an Adfin account. - You have permission to manage API keys. If you cannot see [Settings > Developers](https://console.adfin.com/settings/developer-portal) in the Adfin Console, ask the owner of your account. Platforms and partners Do not use API keys to connect to Adfin accounts that are not yours. Use the [OAuth integration](/products/direct-integration/guides/get-started/oauth) for this. ## Create an API key Create the key in the Adfin Console for the environment that you will use: | Environment | Adfin Console | API base URL | | --- | --- | --- | | Staging | `https://staging.adfin.com` | `https://api.staging.adfin.com/api` | | Production | `https://console.adfin.com` | `https://api.adfin.com/api` | A staging key does not work in production. A production key does not work in staging. ### Step 1: Open the Developer portal 1. Log in to the Adfin Console. 2. Go to [Settings > Developers](https://console.adfin.com/settings/developer-portal). In staging, go to [Settings > Developers](https://staging.adfin.com/settings/developer-portal). 3. Select the **API keys** tab. The **Developer portal** page opens. The **API keys** tab shows a table of your keys. For each key, the table shows the name, the last characters of the key, the permissions, when the key was last used, when it was created, who created it, the expiry date and the status. ### Step 2: Enter the key details 1. Click **Create API key**. The **Create API key** dialog opens. 2. In **Name**, enter a name for the key. Use the name of the system that will use the key, for example `Xero sync worker`. Only your team sees this name. 3. In **Expires**, select when the key stops working: **In 90 days**, **In 180 days** or **In 12 months**. 4. In **Permissions**, select the access that the key needs. Refer to [Permissions](#permissions). 5. Click **Create key**. If a field is not correct, the dialog shows an error. For example, you must enter a name and give the key access to at least one resource. ### Step 3: Copy and store the key The **Copy your new API key** dialog opens. It shows the key and its expiry date. Adfin shows the key one time only. 1. Click **Copy API key**. 2. Store the key in your secrets manager immediately. 3. Click **Copy and close**. The new key shows in the table with the status **Active**. You see the key only one time Adfin does not store the key in a form that it can show again. If you lose the key, revoke it and create a new key. ### Permissions Give each key only the permissions that it needs. For each resource, select one access level: | Access level | What the key can do | | --- | --- | | **None** | The key cannot access the resource. | | **Read** | The key can get and list the resource. | | **Write** | The key can get, list and create the resource. | You can also select a preset: - **Full access**: **Write** access to all resources. - **Read only**: **Read** access to all resources. - **Custom**: You select the access level for each resource. | Resource | Description | | --- | --- | | **Invoices** | Invoices | | **Payment requests** | One-off payment requests | | **Customers** | Customers and their contacts | You cannot change the permissions of a key after you create it. To change the permissions, create a new key, then revoke the old key. ### Key format An API key has this format: ```text adf_{environment}_{key_id}_{secret} ``` - A production key starts with `adf_live_`. - A staging key starts with `adf_stg_`. The prefix helps you and secret-scanning tools find a key that was exposed by accident. ## Authenticate with the API key Send the API key in the `Authorization` header of each request. Use the `Bearer` scheme: ```text Authorization: Bearer {your_api_key} ``` You do not need to get an access token first. The API key is the credential. ### Example: list invoices ```bash Request curl -X GET "https://api.staging.adfin.com/api/invoices" \ -H "Authorization: Bearer $ADFIN_API_KEY" \ -H "Accept: application/json" ``` ### Example: get a customer ```bash Request curl -X GET "https://api.staging.adfin.com/api/customers/{customer_id}" \ -H "Authorization: Bearer $ADFIN_API_KEY" \ -H "Accept: application/json" ``` ### Example: create a customer ```bash Request curl -X POST "https://api.staging.adfin.com/api/customers" \ -H "Authorization: Bearer $ADFIN_API_KEY" \ -H "Content-Type: application/json" \ -d @customer.json ``` The request body is the same as for OAuth. Refer to the API reference for the fields of each request. Keep the key out of your commands The examples read the key from the `ADFIN_API_KEY` environment variable. Do not type the key directly in a command. Your shell can save commands in its history file. ### Errors | Status | Cause | What to do | | --- | --- | --- | | `401 Unauthorized` | The key is missing, not valid, expired or revoked. | Check the `Authorization` header. Check the status of the key in the Adfin Console. If the key is expired or revoked, use a new key. | | `403 Forbidden` | The key does not have the permission for this request. | Check the [permissions](#permissions) of the key. | ### Monitor the requests The **Request logs** tab in **Settings > Developers** shows the requests made with your API keys. For each request, you can see the path, the status, the key, the client IP address and the duration. To find requests, use the filters: - Select **All**, **Succeeded** or **Failed** to filter by status. - Select a key in **Filter by API key** to see only the requests of that key. - Enter a path or a request ID in the search field. Click a request to see its details, for example the error code and the response body. Use the request logs to find failed requests. Also use them to find requests that you do not expect. ## Security considerations An API key gives access to your Adfin account. A person who has your key can read and change your data, within the permissions of the key. Protect each key as you protect a password. ### Keep the key secure - Store the key in a secrets manager or in an encrypted environment variable. - Do not put the key in source code, in a configuration file in a repository, or in a log. - Do not send the key by email, chat or a support ticket. Adfin never asks for your API key. - Do not use the key in a browser, a mobile app or other code that runs on a device of your customer. Use the key only on your servers. - Create one key for each integration. Then you can revoke one key and not stop the other integrations. - Give each key only the permissions that it needs. - For a test, or for a contractor, select an expiry of 90 days or 180 days. ### Expiry Each key stops working at its expiry date. Adfin sends you a notification 30 days, 7 days and 1 day before a key expires. Adfin also adds a task to your dashboard. When a key is expired, requests that use it fail with a `401` error. To continue, select **Regenerate** for the key. You get a new key with the same name and permissions. Then update your systems with the new key. To prevent an interruption, rotate the key before it expires. ### Unused keys If a key is not used for 60 days, Adfin sends you a warning. If the key is not used for 90 days, Adfin revokes it. ### Rotate a key Rotate a key as part of your regular operations, for example before it expires or when a person who had access to it leaves your team. When you rotate a key, you get a new key with the same name and permissions. The old key continues to work for 24 hours, so you can update your systems without an interruption. 1. Go to **Settings > Developers > API keys**. 2. Open the **Actions** menu of the key and select **Rotate**. A dialog opens and asks you to confirm. 3. Click **Rotate key**. A dialog shows the new key and the time when the old key stops working. 4. Copy the new key and store it in your secrets manager. 5. Update all systems that use the key. Do this in less than 24 hours. 6. Make sure that your systems use the new key. On the **Request logs** tab, filter by the key and check the recent requests. Until the old key stops working, the table shows both versions of the key. It also shows the time when the old version stops working. Rotation rules - The new key expires 12 months after the rotation. - If the old key expires in less than 24 hours, it stops working at its expiry date. - You cannot rotate a key again until the old key stops working. ### Revoke a key Revoke a key when you do not need it any more, or when the key is exposed. When you revoke a key, it stops working immediately. Requests that use it fail with a `401` error. You cannot undo this. 1. Go to **Settings > Developers > API keys**. 2. Open the **Actions** menu of the key and select **Revoke**. A dialog opens. It shows when the key was last used. 3. Click **Revoke key**. To cancel, click **Keep key**. The key shows in the table with the status **Revoked**. ### If a key is exposed Do these steps immediately if you think that a key is exposed. For example, the key was committed to a repository, written in a log, or sent to a person who must not have it. Revoke, do not rotate Do not rotate an exposed key. After a rotation, the old key continues to work for 24 hours. Revoke the key. This stops the key immediately. 1. Revoke the key. 2. Create a new key with the same permissions. 3. Update your systems with the new key. 4. On the **Request logs** tab, filter by the revoked key. Look for requests that you do not expect, for example from an unknown IP address. 5. Remove the key from the place where it was exposed. For example, remove it from the repository history or delete the log. 6. If you find requests that you do not expect, contact Adfin support. Do not send the key.