> ## Documentation Index
> Fetch the complete documentation index at: https://docs.metabind.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Optimize Asset

> Optimize a component asset for size and format

Optimize an image asset by converting format, adjusting quality, or resizing.

## Path Parameters

<ParamField path="organizationId" type="string" required>
  Organization ID
</ParamField>

<ParamField path="projectId" type="string" required>
  Project ID
</ParamField>

<ParamField path="componentId" type="string" required>
  Component ID
</ParamField>

<ParamField path="assetId" type="string" required>
  Asset ID
</ParamField>

## Request Body

<ParamField body="options" type="object" required>
  Optimization options (at least one property required)
</ParamField>

<ParamField body="options.quality" type="number">
  Image quality (1-100)
</ParamField>

<ParamField body="options.format" type="string">
  Output format: `jpeg`, `png`, `webp`, or `avif`
</ParamField>

<ParamField body="options.width" type="number">
  Target width in pixels (1-10000)
</ParamField>

<ParamField body="options.height" type="number">
  Target height in pixels (1-10000)
</ParamField>

### Example Request

```json theme={null}
{
  "options": {
    "quality": 85,
    "format": "webp",
    "width": 800
  }
}
```

## Response

Returns the optimized Component Asset object.

```json theme={null}
{
  "id": "asset123",
  "componentId": "c123",
  "name": "hero-image.webp",
  "type": "image/webp",
  "url": "https://cdn.metabind.ai/.../hero-image.webp",
  "size": 45678,
  "status": "active",
  "metadata": {
    "width": 800,
    "height": 600,
    "originalSize": 2048576,
    "optimizedAt": "2024-03-22T10:00:00Z"
  },
  "tags": ["hero"],
  "createdAt": "2024-03-20T10:00:00Z",
  "updatedAt": "2024-03-22T10:00:00Z"
}
```

<Note>
  Optimization replaces the original file. The original size is stored in metadata for reference.
</Note>

## Error Responses

### Unsupported Format

```json theme={null}
{
  "error": {
    "code": "UNSUPPORTED_FORMAT",
    "message": "Cannot optimize this file type",
    "details": {
      "type": "application/pdf"
    }
  }
}
```

### Asset In Use

```json theme={null}
{
  "error": {
    "code": "ASSET_IN_USE",
    "message": "Asset is referenced in published packages and cannot be modified",
    "details": {
      "packages": ["1.0.0"]
    }
  }
}
```

## Code Examples

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.metabind.ai/app/v1/organizations/org123/projects/proj456/components/c123/assets/asset123/optimize" \
    -H "Authorization: Bearer YOUR_JWT" \
    -H "Content-Type: application/json" \
    -d '{
      "options": {
        "quality": 85,
        "format": "webp"
      }
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch(
    'https://api.metabind.ai/app/v1/organizations/org123/projects/proj456/components/c123/assets/asset123/optimize',
    {
      method: 'POST',
      headers: {
        'Authorization': 'Bearer YOUR_JWT',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        options: {
          quality: 85,
          format: 'webp'
        }
      })
    }
  );

  const asset = await response.json();
  console.log(`Optimized: ${asset.metadata.originalSize} -> ${asset.size} bytes`);
  ```
</CodeGroup>
