# Generate Image

Generate chart images using customizable [templates](https://instacharts.io/docs/guides/generate-charts-using-templates/) and data parameters.

## Endpoint

```
POST https://api.instacharts.io/v1/generate/image
```

## Authorization

This endpoint requires [OAuth 2.0 authentication](https://instacharts.io/docs/api/authentication/). Include your access token in the Authorization header:

```
Authorization: Bearer your_access_token
```

## Request Parameters

### Headers

| Header           | Required | Description                                                   |
|------------------|----------|---------------------------------------------------------------|
| `Authorization`  | Yes      | Bearer token obtained through OAuth 2.0                      |
| `Content-Type`   | Yes      | Must be `application/json`                                   |

### Body Parameters

| Parameter     | Type   | Required | Description                                                                                     |
|---------------|--------|----------|-------------------------------------------------------------------------------------------------|
| `templateId`  | string | Yes      | Unique identifier of the chart template                                                         |
| `chartType`   | string | No       | Type of chart (default: “stackedBar”). [Chart Types](https://instacharts.io/docs/automation/chart-templates/#chart-type-values) |
| `x`           | array  | Yes      | X-axis data object Ex: `["Mobile", "Desktop", "Tablet"]`                             |
| `y`           | array  | Yes      | Y-axis data object Ex: `[100, 200, 400]`                                                        |
| `z`           | array  | Yes (if unaggregated data is chosen and the chart type has a breakdown) | Breakdown axis data object Ex: `["Site A", "Site A", "Site B"]`                   |
| `series`      | array  | Yes (if series data is chosen)       | Series data. An array of objects each with a label and data array Ex:`[{label:"Site A",data:[1,2,3]},{label:"Site B",data:[4,5,6]}]` |
| `title`       | string | No       | Main chart title                                                                             |
| `subtitle`    | string | No       | Secondary chart title                                                                         |
| `xTitle`      | string | No       | X-Axis title                                                                                  |
| `yTitle`      | string | No       | Y-Axis title                                                                                  |
| `zTitle`      | string | No       | Legend title                                                                                  |
| `dLabel`      | string | No       | Show Data Labels near the chart pieces Ex: dLabel=true                                        |
| `overrides`   | string | No       | Any [query params](https://instacharts.io/docs/automation/chart-templates/#available-parameters) not defined above. Join parameters with `&` Ex: `color=cccccc&width=500` |

## Example Requests

### Basic Line Chart

```
{
    "templateId": "basic-line",
    "chartType": "line",
    "title": "Monthly Sales 2024",
    "subtitle": "Revenue by Product Category",
    "x": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
    "xTitle": "Month",
    "yTitle": "Revenue ($)",
    "y": [1200, 1900, 2100, 2400, 2800, 3100]
}
```

## Responses

### Success Response

```
HTTP/1.1 200 OK

Content-Type: image/png
```

The response body will contain the binary image data in the requested format.

### Error Responses

#### Invalid Request (400)

```
{
    "error": "invalid_request",
    "message": "Required parameter 'templateId' is missing",
    "code": "MISSING_PARAM"
}
```

#### Authentication Error (401)

```
{
    "error": "unauthorized",
    "message": "Invalid or expired access token",
    "code": "INVALID_TOKEN"
}
```

#### Template Not Found (404)

```
{
    "error": "not_found",
    "message": "Template with ID 'basic-line' not found",
    "code": "TEMPLATE_NOT_FOUND"
}
```
