> ## 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 new, optimized Component Asset object.

```json theme={null}
{
  "data": {
    "id": "asset456",
    "componentId": "c123",
    "name": "hero-image_optimized.webp",
    "type": "image/webp",
    "url": "https://cdn.metabind.ai/.../hero-image_optimized.webp",
    "size": 45678,
    "status": "active",
    "metadata": {
      "width": 800,
      "height": 600,
      "format": "webp",
      "originalAssetId": "asset123",
      "optimizationParams": {
        "quality": 85,
        "format": "webp",
        "width": 800
      }
    },
    "tags": ["hero"],
    "createdAt": "2024-03-22T10:00:00Z",
    "updatedAt": "2024-03-22T10:00:00Z"
  }
}
```

<Note>
  Optimization creates a new asset with a new ID, named `<base>_optimized.<ext>` (for example, `hero-image_optimized.webp`). The original asset is not changed; its ID is stored in the new asset's `metadata.originalAssetId`.
</Note>

## Error Responses

### Unsupported Format

```json theme={null}
{
  "error": {
    "code": "VALIDATION_FAILED",
    "message": "Validation failed",
    "details": {
      "assetType": "Optimization is only supported for image assets"
    }
  }
}
```

### 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 { data: asset } = await response.json();
  console.log(`Optimized copy: ${asset.name} (${asset.size} bytes)`);
  ```
</CodeGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.