# Test a rule on the last days

`POST https://api.fiscalisto.com/v1/rules/{id}/backtest`

On every record of its trigger from the last days days (90 by default, up to 2,000): how many it holds on, what they add up to and what it would have done. Nothing changes.

## Parameters

| Name | In | Required | Description |
| --- | --- | --- | --- |
| `id` | path | yes | The object's id. |
| `Idempotency-Key` | header |  | Optional (the SDKs send it on their own). If the response is lost, repeat the request with the same value within 24 hours: it answers the same, with idempotent-replayed: true, and doesn't do it twice (it doesn't create, send emails or charge). |

## Body

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `days` | integer |  | How many days back (90 by default, up to 90). (from 1 to 90) |

Example:

```json
{
  "days": 90
}
```

## With the TypeScript SDK

```ts
import { Fiscalisto } from "fiscalisto";

// La llave viene de FISCALISTO_API_KEY.
const fiscalisto = new Fiscalisto();

const ruleBacktest = await fiscalisto.rules.backtest("id", { days: 90 }, { idempotencyKey: "pedido-A-1042" });
```

## With the Python SDK

```python
from fiscalisto import Fiscalisto

# La llave viene de FISCALISTO_API_KEY.
fiscalisto = Fiscalisto()

rule_backtest = fiscalisto.rules.backtest("id", {"days": 90}, idempotency_key="pedido-A-1042")
```

## With the PHP SDK

```php
<?php

use Fiscalisto\Fiscalisto;

// La llave viene de FISCALISTO_API_KEY.
$fiscalisto = new Fiscalisto();

$ruleBacktest = $fiscalisto->rules->backtest('id', ['days' => 90], ['idempotency_key' => 'pedido-A-1042']);
```

## With the .NET SDK

```csharp
using Fiscalisto;

// La llave viene de FISCALISTO_API_KEY.
var fiscalisto = new FiscalistoClient();

var ruleBacktest = await fiscalisto.Rules.BacktestAsync("id", new BacktestRuleParams { Days = 90 }, new RequestOptions { IdempotencyKey = "pedido-A-1042" });
```

## With the Go SDK

```go
package main

import (
	"context"
	"fmt"
	"log"

	fiscalisto "fiscalisto.com/go"
)

func main() {
	// La llave viene de FISCALISTO_API_KEY.
	client, err := fiscalisto.NewClient("")
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()

	ruleBacktest, err := client.Rules.Backtest(ctx, "id", &fiscalisto.BacktestRuleParams{Days: fiscalisto.Int64(90)}, fiscalisto.WithIdempotencyKey("pedido-A-1042"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", *ruleBacktest)
}
```

## With the Java SDK

```java
import com.fiscalisto.FiscalistoClient;
import com.fiscalisto.RequestOptions;
import com.fiscalisto.model.BacktestRuleParams;

// La llave viene de FISCALISTO_API_KEY.
var fiscalisto = new FiscalistoClient();

var ruleBacktest = fiscalisto.rules().backtest("id", new BacktestRuleParams().setDays(90), RequestOptions.idempotencyKey("pedido-A-1042"));
System.out.println(ruleBacktest);
```

## Response 200

OK.

A `RuleBacktest` object:

| Field | Type | Always present | Description |
| --- | --- | --- | --- |
| `object` | `"rule_backtest"` | yes | What kind of object it is («invoice», «customer»…). |
| `rule` | string | yes | The rule (rul_…). |
| `days` | integer | yes | How many days back it looked. (from -9007199254740991 to 9007199254740991) |
| `since` | string | yes | From which day (YYYY-MM-DD, Mexico time). |
| `records` | integer | yes | How many invoices, orders, movements or notices of its trigger it looked at. (from -9007199254740991 to 9007199254740991) |
| `matches` | integer | yes | On how many its conditions would have held. (from -9007199254740991 to 9007199254740991) |
| `amount` | number or null | yes | What the matching ones add up to, in pesos; null for SAT notices or if none matches. |
| `capped` | boolean | yes | true: it looked at the 2,000 most recent and stopped there. |
| `would_do` | array of string | yes | What it would have done with each matching one, in words. |
| `else_would_do` | array of string | yes | What it would have done with the rest («Si no se cumple»), in words. |
| `wait_days` | integer | yes | Days it waits and checks again before acting (0: acts right away). (from -9007199254740991 to 9007199254740991) |

```json
{
  "object": "rule_backtest",
  "rule": "rul_OufpKqnrvx9p2Hq8vR4nXk7LmT3wYc9pB6",
  "days": 90,
  "since": "2026-08-24",
  "records": 412,
  "matches": 37,
  "amount": 1284300.5,
  "capped": false,
  "would_do": [
    "poner el centro de costo «Obra Norte»"
  ],
  "else_would_do": [],
  "wait_days": 0
}
```

## Errors

- `feature_not_in_plan` (403): Live, this part of the API (inventario, portal de proveedores, pedidos, comprobación de gastos) follows the empresa's plan, as in the app: it comes with Operaciones (added to any paid plan; included in Corporativo). The books (the /v1/accounting routes except DIOT, cross checks, IVA refunds and fixed assets) come with Contabilidad (added to Independiente, Emprendedor or Negocio; included in Despacho and Corporativo): without it, changing the books answers this error, and reading them works only up to the last month it had it. Add either in Ajustes › Plan y pagos; test mode has everything.
- `issuer_archived` (400): The issuer is archived and no longer stamps. Its documents can still be read and canceled.
- `issuer_linked_empresa` (403): That issuer is an empresa of the app connected to your account: through the API it only offers its Portal de proveedores (/v1/portal_suppliers and /v1/supplier_invoices) and, once someone who may pay suppliers and change Configurar pagos turned them on, its supplier payouts (/v1/payouts and /v1/payroll_payouts). It also stops when whoever connected it (or turned its payouts on) no longer has that permission in the empresa: message_es says which. Do everything else in the app.
- `issuer_missing` (404): There is no such issuer in the account (or it belongs to the other mode).
- `issuer_not_allowed` (403): This key can't use that issuer.
- `issuer_required` (400): The account has several issuers and the request doesn't say which: send `issuer` (iss_…).
- `livemode_mismatch` (400): The object belongs to the other mode: an fk_test_ key only sees test data and an fk_live_ key only live data.
- `resource_missing` (404): There's no object with that id (or it belongs to another issuer or the other mode).

Plus those of any call (key, fields, limits): https://fiscalisto.com/en/docs/api/errores.md.
