- Docs
- Get started
- Create an API key
#Create an API key
By the end of this page, you can authenticate requests to the Ledgerline API with a test-mode secret key that never appears in your source code.
#Before you begin
To follow this page, you need:
- A Ledgerline account. Test mode works as soon as you sign up, before your business is verified.
- The Owner, Admin, or Developer role on that account. Other roles can see the key list but can’t create keys.
- curl, Node.js 18 or later, or Python 3.9 or later, to run the examples.
#Choose a key type
Ledgerline issues three types of key. The prefix tells you the type and the mode, so you can recognize a key that turns up in a log file or a pull request.
sk_test_7Hq2mVx9LpR4tKw8NcZ3bF6d
skTypeskfor secret,rkfor restricted,pkfor publishable.testModetestorlive. A key can’t switch modes after you create it.7Hq2mVx9LpR4tKw8NcZ3bF6dSecret- 24 random characters, shown in full once, when you create the key.
| Key | Prefixes | Use it from | What it can do |
|---|---|---|---|
| Secret | sk_test_ sk_live_ |
Your server | Everything your account can do through the API, including moving money. |
| Restricted | rk_test_ rk_live_ |
Your server, scripts, and third-party tools | Only the resources and access levels you grant. |
| Publishable | pk_test_ pk_live_ |
Browsers and mobile apps | Turn card details into a token. Nothing else. |
On this page you create a secret key in test mode. Test keys can only reach test data, so no card is charged and no money moves, whatever you send.
#Create a key in the Dashboard
- Sign in to the Ledgerline Dashboard and open Developers › API keys.
- Check that Test mode, in the top-right corner, is on. A key belongs to the mode that’s active when you create it.
- Click Create secret key.
- Enter a name that says where the key will run, such as
checkout-server-dev. The name appears in the key list and the audit log, never in requests. - Click Create, then click Copy next to the new key.
Ledgerline shows the full key once, in the dialog that opens when you create it. If you close the dialog without copying the key, delete that key and create a new one. Nobody at Ledgerline can retrieve it for you.
When you’re ready to take real payments, repeat these steps with Test mode off. That gives you a live key that starts with sk_live_. Live keys are available once your account is verified.
#Store the key in an environment variable
Your code reads the key from an environment variable when it runs. The key stays out of version control, and you can switch from a test key to a live key without changing code.
Run these commands in your project directory. Replace the example key with the one you copied.
# Save the key to .env and keep that file out of Git
echo 'LEDGERLINE_SECRET_KEY=sk_test_7Hq2mVx9LpR4tKw8NcZ3bF6d' >> .env
echo '.env' >> .gitignore
# Load the variables from .env into this terminal session
set -a; source .env; set +a
In production, keep the key in your hosting platform’s secret manager instead of a file on the server. Give each service its own key, so you can revoke one without touching the rest.
#Check that the key works
Send the key as a bearer token in the Authorization header. GET /v1/api_keys/current returns details about the key that made the request. It reads no customer data and changes nothing, so it’s a safe first call.
curl https://api.ledgerline.com/v1/api_keys/current \
-H "Authorization: Bearer $LEDGERLINE_SECRET_KEY"
// npm install @ledgerline/node
// Save as check-key.mjs, then run: node check-key.mjs
import Ledgerline from "@ledgerline/node";
const ledgerline = new Ledgerline(process.env.LEDGERLINE_SECRET_KEY);
const key = await ledgerline.apiKeys.retrieveCurrent();
console.log(key);
# pip install ledgerline
import os
import ledgerline
client = ledgerline.Client(api_key=os.environ["LEDGERLINE_SECRET_KEY"])
key = client.api_keys.retrieve_current()
print(key)
{
"id": "key_01JD8M4Q2W7XKZ3N9PVR6T5B8C",
"object": "api_key",
"name": "checkout-server-dev",
"type": "secret",
"livemode": false,
"secret_last4": "bF6d",
"permissions": "all",
"allowed_ips": [],
"created_at": "2026-11-18T09:42:17Z",
"last_used_at": "2026-11-18T09:44:03Z",
"expires_at": null,
"metadata": {}
}
"livemode": false confirms that you’re using a test key. The response never includes the secret itself, only its last four characters in secret_last4.
#Create a restricted key with the API
Give each service only the access it needs. A nightly job that reports on payouts, for example, needs to read payouts and nothing more. You can create restricted keys in the Dashboard, or from code with POST /v1/api_keys. Authenticate the request with a secret key, or with a restricted key that has api_keys: write.
#Parameters
| Parameter | Description |
|---|---|
name |
A label for the key list and the audit log, 1 to 64 characters. Requests never include it. |
permissions |
Maps each resource to none, read, or write. Resources you leave out get none. Restricted key permissions lists every resource. |
allowed_ips |
IPv4 or IPv6 CIDR ranges that may use the key. Requests from any other address fail with ip_not_allowed. Leave it out to allow any address. |
expires_at |
When the key stops working, in UTC. Leave it out for a key that works until you revoke it. |
metadata |
Up to 20 key-value pairs for your own records. Ledgerline stores them and never acts on them. |
Send an Idempotency-Key header with a unique value, such as a UUID. If the connection drops and you retry with the same value, Ledgerline returns the key it already created instead of making a second one.
curl https://api.ledgerline.com/v1/api_keys \
-H "Authorization: Bearer $LEDGERLINE_SECRET_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 5f1c2e9a-8b4d-4c3e-a6f7-0d9b2c1e4a73" \
-d '{
"name": "payouts-report-cron",
"permissions": {
"payouts": "read",
"balance": "read"
},
"allowed_ips": ["203.0.113.24/32"],
"expires_at": "2027-11-18T00:00:00Z",
"metadata": { "owner": "finance-eng" }
}'
// npm install @ledgerline/node
// Save as create-key.mjs, then run: node create-key.mjs
import Ledgerline from "@ledgerline/node";
const ledgerline = new Ledgerline(process.env.LEDGERLINE_SECRET_KEY);
const key = await ledgerline.apiKeys.create(
{
name: "payouts-report-cron",
permissions: { payouts: "read", balance: "read" },
allowed_ips: ["203.0.113.24/32"],
expires_at: "2027-11-18T00:00:00Z",
metadata: { owner: "finance-eng" },
},
{ idempotencyKey: "5f1c2e9a-8b4d-4c3e-a6f7-0d9b2c1e4a73" }
);
console.log(key);
# pip install ledgerline
import os
import ledgerline
client = ledgerline.Client(api_key=os.environ["LEDGERLINE_SECRET_KEY"])
key = client.api_keys.create(
name="payouts-report-cron",
permissions={"payouts": "read", "balance": "read"},
allowed_ips=["203.0.113.24/32"],
expires_at="2027-11-18T00:00:00Z",
metadata={"owner": "finance-eng"},
idempotency_key="5f1c2e9a-8b4d-4c3e-a6f7-0d9b2c1e4a73",
)
print(key)
{
"id": "key_01JD8P7C5N2R4WQ8XK6M3T9VZA",
"object": "api_key",
"name": "payouts-report-cron",
"type": "restricted",
"livemode": false,
"secret": "rk_test_Vb4nQ8sK2xT7mR9pL3wZ6yH1",
"secret_last4": "6yH1",
"permissions": {
"balance": "read",
"payouts": "read"
},
"allowed_ips": ["203.0.113.24/32"],
"created_at": "2026-11-18T10:05:31Z",
"last_used_at": null,
"expires_at": "2027-11-18T00:00:00Z",
"metadata": {
"owner": "finance-eng"
}
}
secret appears in this response and nowhere else. Save it to your secret manager before you do anything else with the response. Later requests for this key return secret_last4 only.
#Fix authentication errors
When Ledgerline rejects a key, the response has a 401 or 403 status and an error object. Here’s the response to GET /v1/api_keys/current made with a revoked key:
{
"error": {
"type": "authentication_error",
"code": "invalid_api_key",
"message": "This API key was revoked on 2026-11-20T14:02:51Z.",
"doc_url": "https://docs.ledgerline.com/errors#invalid_api_key",
"request_id": "req_7KxP2mQ9vW4n"
}
}
Use the code to find the cause:
| Status | Code | What to check |
|---|---|---|
| 401 | missing_api_key |
The request has no Authorization header, or the header doesn’t start with Bearer. Check that LEDGERLINE_SECRET_KEY is set in the process that sends the request, not only in your terminal. |
| 401 | invalid_api_key |
The key is mistyped, revoked, or expired. Compare its last four characters with the key list in the Dashboard. |
| 403 | permission_denied |
A restricted key is missing a permission the endpoint needs. The message names the permission. |
| 403 | ip_not_allowed |
The request came from an address outside the key’s allowed_ips. The message includes the address Ledgerline saw. |
| 403 | publishable_key_used |
You sent a pk_ key to an endpoint that needs a secret or restricted key. |