# FASHN Agent API recipe: Shopify product videos (v1)

A 10 second product video that starts on the product's front photo and turns to end on its back photo, made by the FASHN Agent from the photos already on a Shopify product page, then added to that product page. One product, or every product in the store.

## Instructions for Claude Code

The user pasted a line asking you to follow this file, with a Shopify product link or a request for their whole store. They don't need to read it. Follow these steps in order, and don't skip the questions.

"This folder" always means the folder you are running in (your working directory), never the folder this file came from. Look for the `.env` there, and save the videos there.

### 1. The FASHN API key
- Look for `FASHN_API_KEY` in the environment, then in a `.env` file in your working directory.
- If it's missing: if there's no `.env` in your working directory, create one containing exactly one line, `FASHN_API_KEY=`. If a `.env` already exists, leave everything in it unchanged and add the line `FASHN_API_KEY=` at the end, unless an empty `FASHN_API_KEY=` line is already there. Then open the `.env` file in the user's default text editor (on a Mac `open -e .env`, on Windows `notepad .env`, on Linux `xdg-open .env`). If it won't open, tell them where the file is.
- Then stop and tell the user, in plain words: go to https://app.fashn.ai/api, click Create new API key, copy it, paste it right after `FASHN_API_KEY=` in the file that just opened, save it, and reply "done".
- Never ask the user to paste the key into the chat. Never print the key, and never write it anywhere else.

### 2. Connect the Shopify store (once)
- The store's address is the `something.myshopify.com` domain. If the user gave a product link on their own domain, ask for it, or read it from the product page's `.json` (`<product link>.json`) if the store is public. Say it back to them.
- Check that Shopify CLI is installed (`shopify version`, version 3.93 or newer). If not: `npm install -g @shopify/cli@latest` (needs Node.js).
- Check whether you're already connected: run a small read query (step 3's product query) with `shopify store execute`. If it works, skip the login.
- If it fails with an authentication error, log in to the store: `shopify store auth --store <store>.myshopify.com --scopes read_products,write_products,read_files,write_files`. Their browser opens a Shopify page; tell them to click Install (or Update), then come back.
- Every store call after this: `shopify store execute -s <store>.myshopify.com --query-file <file.graphql> --variable-file <vars.json> --json`, with `--allow-mutations` for changes. GraphQL Admin API only.

### 3. Pick the photos
- Get the product from its link: the handle is the part after `/products/`. Query:
  ```graphql
  query($h: String!) { productByHandle(handle: $h) { id title onlineStoreUrl
    media(first: 30) { nodes { mediaContentType alt ... on MediaImage { image { url width height } } } } } }
  ```
- Download the images into a folder named after the handle and look at them.
- Image 1: a full body photo of the model wearing the product, from the front. If there's none, the front photo cropped from just above the head to just above the knee. Image 2: the same product from the back.
- Skip close-ups, flat lays, size charts and photos of other products.
- In one message, tell the user which photo is the front and which is the back, and ask them to reply "yes" or change it. Wait for the answer.
- If there's no back photo, say so: the Agent will have to guess the back. Ask whether to go ahead with the front only.
- If the product already has a video, say so and ask before adding another.

### 4. Make the video (start the job, then check on it)
- Check each downloaded file's real type (Shopify's links can end in `.png` and still be JPEG) and convert both to JPEG.
- Make the two photos EXACTLY the same width and height: resize the back photo to the front photo's size. (They are the video's first and last frames; different sizes give dark edges at the start of the video.) If they're over about 4 megapixels, scale both down to the same size, e.g. 1632 × 2448 for a 2:3 photo. Never change the downloaded files; work on copies.
- Turn each into a base64 data URI (`data:image/jpeg;base64,...`).
- Start the job: `POST https://api.fashn.ai/v1/agent/videos/run`
- Headers: `Authorization: Bearer <FASHN_API_KEY>` and `Content-Type: application/json`. Use curl, or set a normal `User-Agent` header if you use Python.
- Body:
  ```json
  {
    "prompt": "<the prompt below, word for word>",
    "images": ["<front>", "<back>"],
    "duration": 10,
    "resolution": "1080p",
    "audio": false
  }
  ```
- The images go in exactly that order: the prompt refers to them by number. Leave out `aspect_ratio`: the video takes the photos' own shape.
- With no back photo, send only the front and use the second prompt below.
- It answers at once with `{"id": "...", "error": null}`. Tell the user the id and that it takes about 5 minutes.
- Check on it every 10 seconds: `GET https://api.fashn.ai/v1/agent/status/<id>`, with the same header. While it runs, `status` is `processing`.
- When `status` is `completed`, the same response has the `predictions` and `credits_used`. If it's `failed`, show the user the `error` and stop.
- Never wait on one open connection for the whole job. If checking stops working, the user can check the same id later; never start a second job without asking.

The prompt, word for word:

> Make a silent 10 second video of the model wearing this product. The video starts exactly on the first image and ends exactly on the second image: the model stands in place and turns slowly to show the back, with a still camera. The first image shows the front of the product and the second image shows the back of the product.

With no back photo (the Agent will have to imagine the back):

> Make a silent 10 second video of the model wearing this product. The video starts exactly on the first image: the model stands in place and turns slowly to show the back, with a still camera. The first image shows the front of the product.

Why this way: the video's first frame is the real front photo and its last frame is the real back photo, so the product keeps its exact colours and its real back. It ends on the back; a looping video player goes straight from the back to the front again.

### 5. Save the video
- Download the result (`predictions[].results[0]`; at 1080p it comes back about 1174 × 1762 for a 2:3 photo) as `<handle>.mp4` in your working directory. Never overwrite: if it exists, use `<handle>-2.mp4` and so on. Below, `<video file>` means that file.
- Tell the user where it is and suggest they watch it before it goes on the page. Ask: "Add it to the product page?" Wait for "yes".

### 6. Add it to the product page
Video can't be added from a link; it needs a staged upload. All through `shopify store execute`:
1. `stagedUploadsCreate` with `input: [{ filename: "<handle>.mp4", mimeType: "video/mp4", resource: VIDEO, fileSize: "<bytes of the video file>", httpMethod: POST }]`. Keep `url`, `resourceUrl` and `parameters`.
2. Upload the file with curl: `curl -X POST`, every parameter as `--form-string "name=value"`, then the file last as `-F "file=@<video file>"`, to `url`. A 2xx answer means it worked.
3. `fileCreate` with `files: [{ originalSource: "<resourceUrl>", contentType: VIDEO, alt: "<product title> video" }]`. Keep the file `id`.
4. Every 5 seconds: `node(id: "<file id>") { ... on Video { fileStatus fileErrors { message } } }` until `READY` (usually under 30 seconds). If `FAILED`, show the errors and stop.
5. `fileUpdate` with `files: [{ id: "<file id>", referencesToAdd: ["<product id>"] }]`.
- If Shopify answers that the file isn't supported on trial accounts, tell the user: Shopify only takes video uploads on a paid plan.
- The video goes to the end of the product's gallery. Give the user the product page link: `onlineStoreUrl`, or if that's empty (it is while the store has a password), `https://<store>.myshopify.com/products/<handle>`.

### 7. The whole store
- List the products: `products(first: 250) { nodes { id handle title onlineStoreUrl } }`.
- Tell the user how many products there are, that each one is one video job, that video uses far more credits than photos, and that you'll first make ONE product at 480p for them to check, then the rest at 1080p. Ask them to confirm before you start, or to pick some products.
- Ask about the photos for all products in one message (one line each).
- The test: make the first product's video exactly as in step 4 but with `"resolution": "480p"`, save it, and ask "Looks right? Make all <N> at 1080p?" Wait for the answer. Don't add the 480p test video to any page.
- Then do steps 4 to 6 for every product at 1080p (including the first), and ask about adding them to the pages once, after all the videos are done.
- One product (a single link) is always 1080p straight away, no test.
- Run at most 4 video jobs at the same time; start the next when one finishes.
- At the end, list every product with its page link, and any that failed and why.
