# Authentication and keys

> How your system identifies itself with its secret key, how to restrict a key to some issuers, to read-only or to a time limit, and how to roll it without downtime.

Every request carries your secret key in the `Authorization` header:

```bash tab="cURL"
curl https://api.fiscalisto.com/v1/account \
  -H "Authorization: Bearer $FISCALISTO_API_KEY"
```

```ts tab="TypeScript"
import { Fiscalisto } from "fiscalisto";

// The key comes from FISCALISTO_API_KEY.
const fiscalisto = new Fiscalisto();

const account = await fiscalisto.account.get();
```

```python tab="Python"
from fiscalisto import Fiscalisto

# The key comes from FISCALISTO_API_KEY.
fiscalisto = Fiscalisto()

account = fiscalisto.account.get()
```

```php tab="PHP"
<?php

use Fiscalisto\Fiscalisto;

// The key comes from FISCALISTO_API_KEY.
$fiscalisto = new Fiscalisto();

$account = $fiscalisto->account->get();
```

```go tab="Go"
package main

import (
	"context"
	"fmt"
	"log"

	fiscalisto "fiscalisto.com/go"
)

func main() {
	// The key comes from FISCALISTO_API_KEY.
	client, err := fiscalisto.NewClient("")
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()

	account, err := client.Account.Get(ctx)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(account.ID)
}
```

```java tab="Java"
import com.fiscalisto.FiscalistoClient;

// The key comes from FISCALISTO_API_KEY.
var fiscalisto = new FiscalistoClient();

var account = fiscalisto.account().get();
System.out.println(account.getId());
```

```csharp tab="C#"
using Fiscalisto;

// The key comes from FISCALISTO_API_KEY.
var fiscalisto = new FiscalistoClient();

var account = await fiscalisto.Account.GetAsync();
```

It's also accepted as the HTTP Basic user, with no password (`curl -u "$FISCALISTO_API_KEY:"`), for tools that only know that way.

## Format

A key is `fk_test_` or `fk_live_` followed by 40 letters and digits. The prefix says the mode (see [Test and live](https://fiscalisto.com/en/docs/api/pruebas-y-produccion)). Fiscalisto keeps only its fingerprint: if you lose it, create another.

## Where to keep it

- In an environment variable or your server's secret manager, never in code or in the repository.
- Never in the browser or in an app installed on phones: whoever has it stamps in your name. The API doesn't answer browser requests (it refuses them with `browser_not_supported`); the exception is **Probar** in this documentation's reference, and only with test keys.
- One key per system or environment, with a name that says where it lives, so you revoke only the one that leaked.

## Keys restricted to some issuers

When creating a key in **Desarrolladores › Llaves** you can check **Solo para algunos emisores** (only for some issuers). That key only sees and stamps with those RFCs; anything touching another issuer, creating new issuers or account-wide (like webhooks) answers `403` with `key_restricted`. It's useful, for example, to give each branch or each customer of your platform a key that only invoices with its RFC.

## Read-only keys

In **Permisos** choose **Solo lectura** for a key that reads and changes nothing: every `GET` request (invoices, PDF, XML, balance, catalogs, events) and the `POST`s that only check: `POST /v1/invoices/preview`, `POST /v1/tax_ids/validate`, `POST /v1/sat_lists/check`, `POST /v1/payroll_receipts/preview` and `POST /v1/payroll_receipts/external/preview`. Everything else (stamping, cancelling, sending, creating or changing data) answers `403` with `key_read_only`. It's useful for a dashboard, a report or your support team's tool. It can be combined with **Solo para algunos emisores**.

## Keys that expire

In **Vence** (expires) choose whether the key stops working in 30 days, 90 days, 6 months or 1 year (**Nunca**, never, otherwise). We email the account's owners and admins 7 days and 1 day before. Once expired it answers `401` with `invalid_api_key`. **Cambiar llave** (roll key) gives you a new one with the same term, counted from that day. `GET /v1/account` says of the key that made the request whether it's read-only (`key.read_only`) and when it expires (`key.expires_at`), so your system can warn ahead.

## Choosing the issuer

Whatever creates documents, customers or products carries the issuer (`iss_…`) in `issuer`, in the body; lists carry it in the query (`?issuer=iss_…`). If the account, or the key, has a single issuer, you can leave it out. If it has several and you don't send it, the API answers `issuer_required`. Everything else's ids (`inv_…`, `cus_…`) already say which issuer they belong to.

## Rolling a key without downtime

In **Desarrolladores › Llaves** choose **Cambiar llave** in its menu. We give you a new one with the same name and permissions, and you choose when the old one stops working: right now, in 1 hour, in 24 hours or in 7 days. Put the new one on your server within that time. If a key leaked, roll it with **Ahora mismo** (right now) or revoke it.

## Limits

- Up to 50 keys per mode.
- Creating live keys requires the user's two-step verification.
- An IP address that sends many invalid keys in a row gets `429` for a minute; it never affects valid keys.

## Authentication errors

| Code | What happened |
| --- | --- |
| [`missing_api_key`](https://fiscalisto.com/en/docs/api/errores#missing_api_key) | The `Authorization` header didn't arrive. |
| [`invalid_api_key`](https://fiscalisto.com/en/docs/api/errores#invalid_api_key) | The key doesn't exist, was revoked or expired. |
| [`key_restricted`](https://fiscalisto.com/en/docs/api/errores#key_restricted) | The key is restricted to other issuers. |
| [`key_read_only`](https://fiscalisto.com/en/docs/api/errores#key_read_only) | The key is read-only and the request changes something. |
| [`livemode_mismatch`](https://fiscalisto.com/en/docs/api/errores#livemode_mismatch) | The object belongs to the other mode. |
| [`account_suspended`](https://fiscalisto.com/en/docs/api/errores#account_suspended) | We suspended the account (or its live keys); the message says why. |
