---
type: api
title: Predict future behavior of a time series
description: Forecasts are not supported for jobs that perform population analysis; an
url: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/operation/operation-ml-forecast
resource: https://www.elastic.co/elastic/docs-builder/docs/4493/api/doc/elasticsearch/operation/operation-ml-forecast
products:
  - Elasticsearch
---

# Predict future behavior of a time series

Availability: Generally available since 6.1

## Paths

- `POST` `/_ml/anomaly_detectors/{job_id}/_forecast`

#### Path parameters

- `job_id` (string) — required
  Identifier for the anomaly detection job. The job must be open when you
create a forecast; otherwise, an error occurs.

## Description

Forecasts are not supported for jobs that perform population analysis; an
error occurs if you try to create a forecast for a job that has an
`over_field_name` in its configuration. Forcasts predict future behavior
based on historical data.

## Required authorization

- Cluster privileges: `manage_ml`

## Authentication

- Api key: `Authorization: <value>`
  Elasticsearch APIs support key-based authentication.
  You must create an API key and use the encoded value in the request header.
  For example:
  
  
  ```
  curl -X GET "${ES_URL}/_cat/indices?v=true" \
    -H "Authorization: ApiKey ${API_KEY}"
  ```
  
  To get API keys, use the `/_security/api_key` APIs.
- Basic: `Authorization: Basic <credentials>`
- Bearer: `Authorization: Bearer <token>`
  Elasticsearch APIs support the use of bearer tokens in the `Authorization` HTTP header to authenticate with the API.
  For examples, refer to [Token-based authentication services](https://www.elastic.co/docs/deploy-manage/users-roles/cluster-or-deployment-auth/token-based-authentication-services)

## Query string parameters

- `duration` (string · default: 1d) — optional
  A period of time that indicates how far into the future to forecast. For
example, `30d` corresponds to 30 days. The forecast starts at the last
record that was processed.
- `expires_in` (string · default: 14d) — optional
  The period of time that forecast results are retained. After a forecast
expires, the results are deleted. If set to a value of 0, the forecast is
never automatically deleted.
- `max_model_memory` (string · default: 20mb) — optional
  The maximum memory the forecast can use. If the forecast needs to use
more than the provided amount, it will spool to disk. Default is 20mb,
maximum is 500mb and minimum is 1mb. If set to 40% or more of the job’s
configured memory limit, it is automatically reduced to below that
amount.

## Request body

`application/json`

- `duration` (string · default: 1d) — optional
  Refer to the description for the `duration` query parameter.
- `expires_in` (string · default: 14d) — optional
  Refer to the description for the `expires_in` query parameter.
- `max_model_memory` (string · default: 20mb) — optional
  Refer to the description for the `max_model_memory` query parameter.

## Response

`application/json`

- `acknowledged` (boolean)
- `forecast_id` (string)
## Code examples

#### Console

```console
POST _ml/anomaly_detectors/low_request_rate/_forecast
{
  "duration": "10d"
}
```

#### JavaScript

```javascript
const response = await client.ml.forecast({
  job_id: "low_request_rate",
  duration: "10d",
});
```

#### Python

```python
resp = client.ml.forecast(
    job_id="low_request_rate",
    duration="10d",
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/_ml/anomaly_detectors/low_request_rate/_forecast" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "duration": "10d"
  }'
```

#### Java

```java
client.ml().forecast(f -> f
    .duration(d -> d
        .time("10d")
    )
    .jobId("low_request_rate")
);
```

#### C#

```c#
var response = await client.MachineLearning
    .ForecastAsync(jobId: "low_request_rate", d1 => d1
        .Duration("10d")
    );
```

#### PHP

```php
$resp = $client->ml()->forecast([
    "job_id" => "low_request_rate",
    "body" => [
        "duration" => "10d",
    ],
]);
```

#### Ruby

```ruby
response = client.ml.forecast(
  job_id: "low_request_rate",
  body: {
    "duration": "10d"
  }
)
```

