skip to Main Content

So just to clarify what I am trying to do.

I do NOT want to upload an image to a product.

I want to use the File API to upload a file to files

I don’t know graphql very well at all.

I have setup a private app, and set the file scopes for the Admin API.
I have copied the Admin API access token, stored as SHOPIFY_ACCESS_TOKEN

I am using shopify’s php sdk


Context::initialize(
        env('SHOPIFY_API_KEY'),
        env('SHOPIFY_API_SECRET'),
        env('SHOPIFY_APP_SCOPES'),
        env('SHOPIFY_DOMAIN'),
        new FileSessionStorage('/tmp/php_sessions'),
        env('SHOPIFY_API_VERSION'),
        false,
        true,
    );


    $client = new Graphql(env("SHOPIFY_DOMAIN"), env("SHOPIFY_ACCESS_TOKEN"));

    $query = <<<QUERY
  mutation fileCreate($files: [FileCreateInput!]!) {
    fileCreate(files: $files) {
      files {
        alt
        createdAt
      }
    }
  }
QUERY;
    $variables = [
        "files" => [
            "alt" => "this is a cat",
            "contentType" => "IMAGE",
            "originalSource" => "https://placekitten.com/1300/1300",
        ],
    ];

    $response = $client->query(["query" => $query, "variables" => $variables]);

The Query part i copied directly from the FileCreate docs.

The Context::initialize I got from the sdk docs.

The error code that is returning is
401{"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}

I am unsure where to go from here and how to get it working.

Can anyone see where I may have gone wrong?


Edit: 20231112

A huge thanks to @hamzasgd for the awesome info.

I have managed to get it working with PHP, which is great.

My issue auth issue was that I was passing the store front api key, instead of the admin api key into the Context::initialize method.

It should have been this

    Context::initialize(
        env('SHOPIFY_API_KEY'),
        env('SHOPIFY_ACCESS_TOKEN'),
        env('SHOPIFY_APP_SCOPES'),
        env('SHOPIFY_DOMAIN'),
        new FileSessionStorage('/tmp/php_sessions'),
        ApiVersion::LATEST,
        false,
        true,
    );

2

Answers


  1. Here is the actual flow that you need to follow this gist is in javascript for the php the flow is same

    Shopify Files API Create Flow

    Login or Signup to reply.
  2. Thank you @hamzasgd.

    Converted your approach in laravel 10 and used Signify/Shopify package to achieve the similar result. Adding here for future references:

    Shopify Files API Create Flow in Laravel

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search