Creating API Token to set Vary?

Hello,

I need to create an API token, so I can set Vary for my images according to this guide: Enable vary for images · Cloudflare Cache docs

I have tried creating a token, but I don’t see a template for setting Vary so I don’t know if I am setting it up correctly.

I have set it to Zones>All Zones.

When I test the API token it returns:

{“result”:{“id”:“ca2165the ID removed1c63”,“status”:“active”,“not_before”:“2021-10-26T00:00:00Z”,“expires_on”:“2026-01-01T23:59:59Z”},“success”:true,“errors”:,“messages”:[{“code”:10000,“message”:“This API Token is valid and active”,“type”:null}]}curl: (6) Could not resolve host: \ curl: (6) Could not resolve host: \

and when I try to set Vary using the document above, inserting my email and API token, it returns:

{“success”:false,“errors”:[{“code”:7003,“message”:“Could not route to /zones/023e10this ID removed0 c353/cache/variants, perhaps your object identifier is invalid?”},{“code”:7000,“message”:“No route for that URI”}],“messages”:,“result”:null}curl: (6) Could not resolve host:
curl: (6) Could not resolve host:
curl: (6) Could not resolve host:
curl: (6) Could not resolve host: \

I suspect I have not set the token correctly?

I tried sending the command (all on one line) via Windows Command Prompt and inserted my CF email address and the API Key I generated.

It’s the first time I’ve done this so go easy on me :wink:

Thank you,
Tim

Hi Tim,

Don’t worry, this ID isn’t secret - there’s no need to redact it :slightly_smiling_face:

Windows Command Prompt requires you use ^ instead of \ to signal the command will continue on the next line.

There are two ways to authenticate yourself when using the API:

  1. API Token (recommended): Authorization: Bearer <token>
  2. Global API Key and Account Email: X-Auth-Key: <global_api_key> and X-Auth-Email: <account_email>

It sounds like you might have generated an API Token but are using it as your Global API Key - could that be correct? You can read more about the the authentication methods here: https://api.cloudflare.com/#getting-started-requests

1 Like

Hi Albert,

Thank you for your help. It’s appreciated.

  • I wasn’t sure about the ID. :upside_down_face:
  • The ^ for Windows Command is useful to know.
  • And yes, I created an API Token so have changed the command.

First step, I test the API token and it seems to work now :slight_smile:

{“result”:{“id”:“ca2f04famy ID63”,“status”:“active”,“not_before”:“2021-10-26T00:00:00Z”,“expires_on”:“2026-01-01T23:59:59Z”},“success”:true,“errors”:,“messages”:[{“code”:10000,“message”:“This API Token is valid and active”,“type”:null}]}

Next, to set up Vary - I send:

curl -X PATCH "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0 c353/cache/variants" ^ 
-H "Authorization: Bearer **my API Token is here**" ^ 
-H "Content-Type: application/json" ^ 
--data '{"value":{"jpeg":["image/webp","image/avif"],"jpg":["image/webp","image/avif"]}}'

However, this returns:

{"success":false,"errors":[{"code":7003,"message":"Could not route to \/zones\/023e105f4ecef8ad9ca31a8372d0 c353\/cache\/variants, perhaps your object identifier is invalid?"},{"code":7000,"message":"No route for that URI"}],"messages":[],"result":null}

Have I done something else wrong?

I also noticed in the error, the address seems to have a forward and back slash like this \/ which is odd.

Thank you, and again, I really appreciate your help!
Tim

There’s a space in your zone_id 023e105f4ecef8ad9ca31a8372d0 c353 :wink:

The back-slash is used to escape the forward-slash - not sure why they’re doing that but it’s nothing to worry about.

Windows doesn’t like single quotes if I recall correctly. You might have to do this instead:
--data "{\"value\":{\"jpeg\":[\"image/webp\",\"image/avif\"],\"jpg\":[\"image/webp\",\"image/avif\"]}}"

1 Like

Hi Albert,

Thanks again,

  • I found my zone ID and inserted it into the URL (the URL was straight from the documentation)!
  • OK on the back-slash.
  • I tried --data line as it was and with the alternative you suggested.

So here’s that code:

curl -X PATCH " https://api.cloudflare.com/client/v4/zones/783d03**my zone here**4db4005/cache/variants" ^
-H "Authorization: Bearer Z888**my API Token here***8uw" ^
-H "Content-Type: application/json" ^
--data "{\"value\":{\"jpeg\":[\"image/webp\",\"image/avif\"],\"jpg\":[\"image/webp\",\"image/avif\"]}}"

It returns this:

curl: (1) Protocol " https" not supported or disabled in libcurl

Any more clues?

Thank you.
Tim

You have a space inside the quotes before https :slightly_smiling_face:

1 Like

Ohhh oops :confounded:

That’s got it.
Success.

{
  "result": {
    "id": "variants",
    "modified_on": "2021-10-27T14:33:30.846138Z",
    "value": {
      "jpeg": [
        "image/webp",
        "image/avif"
      ],
      "jpg": [
        "image/webp",
        "image/avif"
      ]
    }
  },
  "success": true,
  "errors": [],
  "messages": []
}

Thank you so much for your perseverance!
Maybe CF will add a button inside the control panel for this sometime too.

Have a nice day Albert.

1 Like

No problem :slightly_smiling_face:

Yeah, that would help a lot of people. Using cURL with the API can be a bit tricky if you’ve never done it before.

Thanks - you too!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.