Skip to content
Start here

Create a variant

images.v1.variants.create(VariantCreateParams**kwargs) -> VariantCreateResponse
POST/accounts/{account_id}/images/v1/variants

Specify variants that allow you to resize images for different use cases.

Security
API Token

The preferred authorization scheme for interacting with the Cloudflare API. Create a token.

Example:Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY
API Email + API Key

The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.

Example:X-Auth-Email: user@example.com

The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.

Example:X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194
Accepted Permissions (at least one required)
Images Write
ParametersExpand Collapse
account_id: str

Account identifier tag.

maxLength32
id: str
maxLength99
options: Options

Allows you to define image resizing sizes for different use cases.

fit: Literal["scale-down", "contain", "cover", 2 more]

The fit property describes how the width and height dimensions should be interpreted.

One of the following:
"scale-down"
"contain"
"cover"
"crop"
"pad"
height: float

Maximum height in image pixels.

minimum1
metadata: Literal["keep", "copyright", "none"]

What EXIF data should be preserved in the output image.

One of the following:
"keep"
"copyright"
"none"
width: float

Maximum width in image pixels.

minimum1
never_require_signed_urls: Optional[bool]

Indicates whether the variant can access an image without a signature, regardless of image access control.

ReturnsExpand Collapse
class VariantCreateResponse:
variant: Optional[Variant]
id: str
maxLength99
options: VariantOptions

Allows you to define image resizing sizes for different use cases.

fit: Literal["scale-down", "contain", "cover", 2 more]

The fit property describes how the width and height dimensions should be interpreted.

One of the following:
"scale-down"
"contain"
"cover"
"crop"
"pad"
height: float

Maximum height in image pixels.

minimum1
metadata: Literal["keep", "copyright", "none"]

What EXIF data should be preserved in the output image.

One of the following:
"keep"
"copyright"
"none"
width: float

Maximum width in image pixels.

minimum1
never_require_signed_urls: Optional[bool]

Indicates whether the variant can access an image without a signature, regardless of image access control.

Create a variant

import os
from cloudflare import Cloudflare

client = Cloudflare(
    api_token=os.environ.get("CLOUDFLARE_API_TOKEN"),  # This is the default and can be omitted
)
variant = client.images.v1.variants.create(
    account_id="023e105f4ecef8ad9ca31a8372d0c353",
    id="hero",
    options={
        "fit": "scale-down",
        "height": 768,
        "metadata": "none",
        "width": 1366,
    },
)
print(variant.variant)
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "result": {
    "variant": {
      "id": "hero",
      "options": {
        "fit": "scale-down",
        "height": 768,
        "metadata": "none",
        "width": 1366
      },
      "neverRequireSignedURLs": true
    }
  },
  "success": true
}
Returns Examples
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "result": {
    "variant": {
      "id": "hero",
      "options": {
        "fit": "scale-down",
        "height": 768,
        "metadata": "none",
        "width": 1366
      },
      "neverRequireSignedURLs": true
    }
  },
  "success": true
}