Home / Blog / Amazon Product API in a Google Cloud Run Job: No HTTP, No Auth Header

Amazon Product API in a Google Cloud Run Job: No HTTP, No Auth Header

Run an Amazon Buy Box price-watch as a Cloud Run JOB, a container that runs to completion and exits rather than an HTTP server. client.js and retry.js need zero changes from the TypeScript quickstart. The real traps: the scheduler authenticates with an OAuth token to the Admin API, so there is no header for the job's own code to check, and the platform retries a failed task on its exit code, so the baseline write order keeps a retried task from re-firing an alert it already sent.

November 14, 2026
6 min read
SellerMagnet Team
Share & Bookmark
Terminal showing a Google Cloud Run Job task invoked by Cloud Scheduler calling the SellerMagnet product lookup API

To call an Amazon product API from a Google Cloud Run Job, write a plain Node.js script that reuses the TypeScript quickstart's own client.js and retry.js completely unchanged, runs to completion, and exits - a Cloud Run JOB is a different primitive from every HTTP-triggered platform earlier in this series, including the Cloud Functions version on the same Google Cloud. There is no request, no response, and critically no auth header to check inside the code at all: Cloud Scheduler calls the Cloud Run Admin API directly with an OAuth token, and Google's own IAM check happens before the container ever starts.

Key Takeaways

  • A Cloud Run JOB is a container that runs to completion and exits, not an HTTP server: "the container must exit with exit code 0 when the job has successfully completed, and exit with a non-zero exit code when the job has failed." Every sibling platform in this series (Lambda, Cloudflare, GCP Cloud Functions, Azure, Pipedream, Supabase, Vercel, Zapier, GraphQL, n8n) is the opposite shape: a handler that returns a response.
  • Cloud Scheduler triggers a job execution by calling the Cloud Run Admin API (run.googleapis.com) directly, authenticated with an OAuth access token - documented as the pattern for Google APIs on *.googleapis.com, unlike the OIDC identity token every OTHER platform in this series sends straight to a function's own URL. The calling service account needs the Cloud Run Invoker role, checked by Google's own front end before the container starts, so there is no bearer token or header for this job's own code to compare against at all.
  • Cloud Run retries a failed TASK automatically, up to 3 times by default, based purely on its exit code - a platform-level retry keyed on the exit code itself, not on the caller (pg_cron, a cron job, Cloud Scheduler) deciding to re-invoke like every HTTP-triggered sibling in this series.
  • Each task reads its own shard index from the built-in CLOUD_RUN_TASK_INDEX environment variable and the total task count from CLOUD_RUN_TASK_COUNT; shardFor() keeps every ASIN whose list position modulo the task count equals this task's own index, covering the full watch list exactly once with no overlap across parallel tasks.
  • checkOne() absorbs every real SellerMagnet answer (404, 401, or a billed error that exhausted its retries) and only lets a genuine infrastructure failure (the baseline store itself unreachable) propagate to a non-zero exit code. The baseline is written AFTER it is read and AFTER the alert decision, so a task Cloud Run retries after a crash sees its own prior write as the baseline and never re-fires an alert the earlier attempt already sent.

Why does this job have no auth check in its own code?

Every other scheduled-trigger post in this series needs a line of code that checks a header: pg_cron's apikey, Vercel's CRON_SECRET bearer token, an HTTP cron job's own secret. A Cloud Run Job removes that line entirely, because nothing calls the job's container directly over HTTP. Cloud Scheduler starts an execution by sending POST run.googleapis.com/v2/projects/P/locations/L/jobs/J:run with an OAuth access token from a service account, and Google's Cloud Run Admin API itself checks that the calling identity holds the Cloud Run Invoker role before the container ever starts. By the time this file's code runs, the caller is already authorized - there is no header value left to read.

Each platform's own scheduled-retry contract, as documented for this series (checked live Nov 2026).
PlatformWhat re-invokes a failed/missed run
Lambda / API Gateway (series)EventBridge Scheduler re-invokes per its own retry policy
Azure Functions Timer (series)platform retry policy reruns the WHOLE function from the top
Vercel Cron Job (series)delivery can be missed or duplicated; Vercel never retries a failed call
Cloud Run Job task (this post)the PLATFORM retries the exact failed task, keyed on its exit code, up to 3x

client.js - unmodified from the TypeScript quickstart

// client.js - one fetch-based client, X-Api-Key header, 120 s timeout.

// Overridable only for pointing the client at a local test server; a real
// program never needs to set SELLERMAGNET_API_BASE.
const API_BASE = process.env.SELLERMAGNET_API_BASE ?? "https://sellermagnet-api.com/api/";

/**
 * @typedef {Object} ApiErrorShape
 * @property {number} status
 * @property {string} message
 * @property {number} retryAfter - seconds, 0 when the header was absent.
 */

/** Thrown for any non-2xx response or a failed JSON decode. */
export class ApiError extends Error {
  /** @param {ApiErrorShape} shape */
  constructor({ status, message, retryAfter }) {
    super(`${status}: ${message}`);
    this.name = "ApiError";
    this.status = status;
    this.retryAfter = retryAfter;
  }
}

/**
 * Send one GET to the API and return the decoded `data` field.
 * @template T
 * @param {string} endpoint - e.g. "amazon-product-lookup"
 * @param {Record<string, string>} params
 * @returns {Promise<T>}
 */
export async function get(endpoint, params) {
  const url = new URL(API_BASE + endpoint);
  for (const [key, value] of Object.entries(params)) url.searchParams.set(key, value);

  const controller = new AbortController();
  const timeout = setTimeout(() => controller.abort(), 120_000); // server allows up to ~120 s
  let response;
  try {
    response = await fetch(url, {
      headers: { "X-Api-Key": process.env.SELLERMAGNET_API_KEY ?? "" },
      signal: controller.signal,
    });
  } finally {
    clearTimeout(timeout);
  }

  const retryAfter = Number.parseInt(response.headers.get("retry-after") ?? "0", 10) || 0;
  let body;
  try {
    body = await response.json(); // a proxy's error page is HTML, not JSON
  } catch {
    throw new ApiError({ status: response.status, message: response.statusText, retryAfter });
  }
  if (response.ok && body.success) return body.data;
  throw new ApiError({ status: response.status, message: body.message ?? response.statusText, retryAfter });
}

retry.js - unmodified from the TypeScript quickstart

// retry.js - retries that respect billing: 429/500(once)/502/503 only.

import { ApiError } from "./client.js";

const RETRYABLE = new Set([429, 500, 502, 503]);

/**
 * @template T
 * @param {() => Promise<T>} call
 * @param {number} [attempts]
 * @returns {Promise<T>}
 */
export async function withRetry(call, attempts = 4) {
  let seen500 = false;
  for (let attempt = 1; ; attempt++) {
    try {
      return await call();
    } catch (err) {
      if (!(err instanceof ApiError)) throw err;
      const give500Twice = err.status === 500 && seen500;
      if (!RETRYABLE.has(err.status) || give500Twice || attempt === attempts) throw err;
      seen500 = seen500 || err.status === 500;
      const backoff = 2 ** attempt * 100; // ms, small for the test harness
      const wait = Math.max(backoff, err.retryAfter * 1000);
      await new Promise((resolve) => setTimeout(resolve, wait));
    }
  }
}

Sharding one watch list across parallel tasks

Google's own retries guide cites LIMIT 100 OFFSET $CLOUD_RUN_TASK_INDEX*100 as a sharding pattern for a SQL batch; shardFor() below does the equivalent for a plain array, keeping every ASIN whose index modulo the task count equals this task's own CLOUD_RUN_TASK_INDEX. Each task also reads its own CLOUD_RUN_TASK_ATTEMPT, incremented by Cloud Run on every platform-level retry of that same task, for logging only - the code that keeps a retry safe does not branch on the attempt number at all, it relies on the baseline write order in checkOne().

priceWatch.js - shardFor(), checkOne() and checkBatch()

// priceWatch.js - the price-watch logic behind a Cloud Run JOB task (not a service): the
// container runs to completion and exits, invoked on a schedule by Cloud Scheduler calling
// the Cloud Run Admin API directly (docs.cloud.google.com/run/docs/execute/jobs-on-schedule).
//
// NO HANDLER-LEVEL AUTH CHECK, unlike every HTTP-triggered sibling in this series: Cloud
// Scheduler authenticates to run.googleapis.com with an OAuth access token from a service
// account that holds the Cloud Run Invoker role on this job - Google's own front end
// enforces that before the container ever starts, so there is no bearer token or header for
// this file's code to compare against at all.
//
// EXIT-CODE TRAP: "the container must exit with exit code 0 when the job has successfully
// completed, and exit with a non-zero exit code when the job has failed"
// (docs.cloud.google.com/run/docs/reference/container-contract). Cloud Run retries a failed
// TASK up to 3 times by default (docs.cloud.google.com/run/docs/jobs-retries) based purely on
// that exit code - a platform-level retry keyed on the exit code, not on the caller deciding
// to re-invoke like every HTTP post earlier in this series. checkOne() below absorbs a real
// SellerMagnet answer (404, 401, exhausted retries on a billed error) and returns null instead
// of throwing, so those never trigger a task retry; only a genuine infrastructure failure
// (the baseline store itself unreachable) propagates and sets a non-zero exit code.

import { lookupProduct } from "./lookup.js";
import { withRetry } from "./retry.js";
import { ApiError } from "./client.js";

const MARKETPLACE_ID = "ATVPDKIKX0DER";
const DROP_THRESHOLD = 0.03; // report a Buy Box drop of 3% or more

/** Baseline key, one per ASIN + marketplace pair. */
function baselineKey(asin, marketplaceId) {
  return `price-watch:${marketplaceId}:${asin}`;
}

/**
 * Splits a full watch list into this task's own shard: every ASIN whose index modulo the
 * task count equals this task's own index. docs.cloud.google.com/run/docs/jobs-retries cites
 * `LIMIT 100 OFFSET $CLOUD_RUN_TASK_INDEX*100` as one sharding pattern for SQL; round-robin by
 * index works the same way for a plain in-memory array and spreads unevenly-sized runs more
 * evenly than a contiguous block split.
 * @param {string[]} asins
 * @param {number} taskIndex
 * @param {number} taskCount
 * @returns {string[]}
 */
export function shardFor(asins, taskIndex, taskCount) {
  if (taskCount < 1) throw new Error(`taskCount must be >= 1, got ${taskCount}`);
  if (taskIndex < 0 || taskIndex >= taskCount) {
    throw new Error(`taskIndex ${taskIndex} out of range for taskCount ${taskCount}`);
  }
  return asins.filter((_, i) => i % taskCount === taskIndex);
}

/**
 * @param {{get:(k:string)=>Promise<number|null>, set:(k:string,v:number)=>Promise<unknown>}} store
 * @param {string} asin
 * @param {{log:(s:string)=>void, error:(s:string)=>void}} log
 * @returns {Promise<{asin:string, from:number, to:number, dropPct:number}|null>}
 */
export async function checkOne(store, asin, log) {
  let current;
  try {
    current = await withRetry(() => lookupProduct(asin, MARKETPLACE_ID));
  } catch (err) {
    // A real answer (404 not found, 401 invalid key, or a billed error that exhausted its
    // retries): absorb it and move to the next ASIN. This must NEVER escape checkOne, or a
    // single bad ASIN would set a non-zero exit code and make Cloud Run retry the whole task.
    log.error(`${asin}: ${err instanceof ApiError ? err.message : String(err)}`);
    return null;
  }

  const price = current.buyBoxInfo.price; // null when there is no featured offer (N/A)
  const key = baselineKey(asin, MARKETPLACE_ID);
  const baseline = await store.get(key); // throws on a real store failure - NOT absorbed here

  let alert = null;
  if (price != null && baseline != null) {
    const dropPct = Math.round(((baseline - price) / baseline) * 1000) / 10;
    if (dropPct >= DROP_THRESHOLD * 100) {
      alert = { asin, title: current.title, from: baseline, to: price, dropPct };
    }
  }

  // Write the new baseline AFTER reading it and AFTER deciding on the alert. This is what
  // makes a RETRIED task attempt (CLOUD_RUN_TASK_ATTEMPT > 0, same CLOUD_RUN_TASK_INDEX) safe:
  // if attempt 0 already wrote today's price before crashing on a later ASIN, attempt 1 reads
  // that same price as its baseline and correctly sees no further change, so it never re-fires
  // an alert attempt 0 already sent.
  if (price != null) await store.set(key, price);

  return alert;
}

/**
 * Runs checkOne() over this task's own shard. A store failure (store.get/store.set throwing)
 * is deliberately NOT caught here - it propagates to main.js, which is the only place that
 * decides the task's exit code.
 * @param {{get:(k:string)=>Promise<number|null>, set:(k:string,v:number)=>Promise<unknown>}} store
 * @param {string[]} asins
 * @param {{log:(s:string)=>void, error:(s:string)=>void}} log
 */
export async function checkBatch(store, asins, log) {
  const alerts = [];
  const checked = [];
  for (const asin of asins) {
    const alert = await checkOne(store, asin, log);
    checked.push(asin);
    if (alert) alerts.push(alert);
  }
  for (const alert of alerts) {
    log.log(`ALERT ${alert.asin} ${alert.from} -> ${alert.to} (-${alert.dropPct}%)`);
  }
  return { alerts, checked };
}
Table of Cloud Run job exit codes and whether the platform retries the task, contrasted with the HTTP-trigger retry sources used by every sibling platform in this series
Cloud Run retries a task on its exit code alone - no caller decision involved.

main.js - the job entrypoint: reads the task env vars, runs the shard, sets the exit code

// main.js - the Cloud Run Job's entrypoint. No HTTP server, no listening port: the container
// runs this to completion and exits (docs.cloud.google.com/run/docs/reference/container-contract,
// "Container running in a job execution must exit upon completion").
//
// CLOUD_RUN_TASK_INDEX / CLOUD_RUN_TASK_COUNT shard one watch list across N parallel tasks;
// CLOUD_RUN_TASK_ATTEMPT is logged so a retried attempt is visible in the job's own logs,
// even though no code branches on it directly - safety against a retried attempt comes from
// checkOne()'s baseline-write-after-read order, not from reading this variable.

const WATCH_LIST = ["B0CL61F39H", "B0C77GVB5Y", "B0SECOND01", "B0THIRD001", "B0FOURTH01"];

/**
 * @param {string[]} watchList
 * @param {{get:(k:string)=>Promise<number|null>, set:(k:string,v:number)=>Promise<unknown>}} store
 * @param {{log:(s:string)=>void, error:(s:string)=>void}} log
 * @param {{index:number, count:number, attempt:number}} task
 * @returns {Promise<{exitCode:number, alerts:object[], checked:string[]}>}
 */
export async function runTask(watchList, store, log, task) {
  const { shardFor, checkBatch } = await import("./priceWatch.js");
  const shard = shardFor(watchList, task.index, task.count);
  log.log(`task ${task.index}/${task.count} attempt ${task.attempt}: shard=${JSON.stringify(shard)}`);
  try {
    const result = await checkBatch(store, shard, log);
    return { exitCode: 0, ...result };
  } catch (err) {
    // A genuine infrastructure failure (the baseline store itself unreachable), not a
    // SellerMagnet answer - checkOne() already absorbed every SellerMagnet-side error.
    log.error(`task ${task.index} failed: ${err.message}`);
    return { exitCode: 1, alerts: [], checked: [] };
  }
}

/** Reads the three Cloud Run job environment variables, defaulting like a single-task run. */
export function taskFromEnv(env) {
  return {
    index: Number.parseInt(env.CLOUD_RUN_TASK_INDEX ?? "0", 10),
    count: Number.parseInt(env.CLOUD_RUN_TASK_COUNT ?? "1", 10),
    attempt: Number.parseInt(env.CLOUD_RUN_TASK_ATTEMPT ?? "0", 10),
  };
}

// Real entrypoint (not exercised by the test harness, which calls runTask()/taskFromEnv()
// directly against a fake store): wires process.env and process.exitCode together.
if (import.meta.url === `file://${process.argv[1]}`) {
  const store = await import("./store.js").then((m) => m.openStore());
  const { exitCode } = await runTask(WATCH_LIST, store, console, taskFromEnv(process.env));
  process.exitCode = exitCode;
}

Wiring it to Cloud Scheduler and a Firestore baseline

The entrypoint above is deployed as the job's container image and invoked on a schedule by a Cloud Scheduler job whose HTTP target is the Cloud Run Admin API's own :run endpoint, authenticated with an OAuth token from a service account holding the Cloud Run Invoker role on this job (docs.cloud.google.com/run/docs/execute/jobs-on-schedule). Neither store.js nor the Scheduler configuration makes a call worth faking in a test harness; both only wire together the functions already proven above.

store.js - the baseline via Firestore (illustrative wiring, not separately tested)

// store.js - illustrative wiring only (not exercised by the test harness, which injects an
// in-memory fake directly into runTask()). A Cloud Run Job has no built-in KV of its own the
// way Vercel (Upstash Redis) or Supabase (its own Postgres REST) does, so the baseline lives
// in Firestore, read with the standard Node client library and the job's attached service
// account credentials (Application Default Credentials, no key file to manage).

import { Firestore } from "@google-cloud/firestore";

export function openStore() {
  const db = new Firestore();
  const col = db.collection("price-watch-baselines");
  return {
    async get(key) {
      const doc = await col.doc(key).get();
      return doc.exists ? doc.data().price : null;
    },
    async set(key, price) {
      await col.doc(key).set({ price, updatedAt: new Date().toISOString() });
    },
  };
}
Sequence diagram of Cloud Scheduler calling the Cloud Run Admin API with an OAuth token, which starts a container task that checks its own shard and exits with a status code
The authorization check happens entirely outside the container, before task 0 even starts.
SellerMagnet API status codes, billing and this task's reaction.
StatusMeaningBilledReaction
200SuccessYesUse data
401 / 403Key or creditsNoStop
404Not on this marketplaceYesAbsorbed, logged, never re-thrown
429Concurrency capNoWait Retry-After, retried
500Internal errorNo, refundedRetry once
502 / 503Upstream / breakerYesWait, then retry, capped at 4, then absorbed

Real output from the tested harness (fakefetch.mjs faking globalThis.fetch only, illustrative data)

task 0/1 attempt 0: shard=["B0CL61F39H"]
first-run-no-baseline: PASS
task 0/1 attempt 0: shard=["B0CL61F39H"]
unchanged-no-alert: PASS
task 0/1 attempt 0: shard=["B0CL61F39H"]
ALERT B0CL61F39H 444.99 -> 399 (-10.3%)
real-drop-fires-alert: PASS
task 0/1 attempt 0: shard=["B0SMALL1"]
drop-under-threshold-no-alert: PASS
task 0/1 attempt 0: shard=["B0RISE1"]
price-rose-no-alert-baseline-updates: PASS
task 0/1 attempt 0: shard=["B0NA001"]
no-buybox-offer-na-no-alert-baseline-kept: PASS
task 0/1 attempt 0: shard=["B0UNKNOWN1"]
err: B0UNKNOWN1: 404: Product not found
404-absorbed-exitcode-0: PASS
task 0/1 attempt 0: shard=["B0RATE001"]
429-then-ok-exitcode-0: PASS
task 0/1 attempt 0: shard=["B0ERR500"]
500-refunded-retried-once-exitcode-0: PASS
task 0/1 attempt 0: shard=["B0RETRY502"]
err: B0RETRY502: 503: breaker open
502-503-capped-absorbed-exitcode-0: PASS
task 0/1 attempt 0: shard=["B0NOKEY001"]
err: B0NOKEY001: 401: Invalid or inactive API key.
401-absorbed-exitcode-0: PASS
task 0/1 attempt 0: shard=["B0CL61F39H"]
err: task 0 failed: Firestore unavailable
store-failure-sets-exitcode-1: PASS
sharding-partitions-full-list-no-overlap: PASS
sharding-round-robin-task0: PASS
sharding-round-robin-task1: PASS
single-task-default-env: PASS
task 0/1 attempt 0: shard=["B0DUPTASK"]
ALERT B0DUPTASK 444.99 -> 399 (-10.3%)
task 0/1 attempt 1: shard=["B0DUPTASK"]
retried-attempt-first-fires: PASS
retried-attempt-second-does-not-refire: PASS
out-of-range-task-index-throws: PASS
---
PASS=19 FAIL=0
ALL 19/19 SCENARIOS PASS

Frequently Asked Questions

Does this job check an auth header before calling SellerMagnet?

No, and that is the point: Cloud Scheduler calls the Cloud Run Admin API (run.googleapis.com) with an OAuth access token from a service account that holds the Cloud Run Invoker role on this job. Google's own front end checks that identity before the container ever starts, so unlike every HTTP-triggered platform earlier in this series, there is no bearer token or header value left for this file's own code to compare against.

What makes Cloud Run retry a task, and is that safe for this price-watch?

A task that exits with a non-zero code is retried by the platform itself, up to 3 times by default, independent of Cloud Scheduler. checkOne() only lets a genuine infrastructure failure (the baseline store itself unreachable) propagate to that exit code; a real SellerMagnet answer (404, 401, or a billed error that exhausted its retries) is absorbed and logged. A retried attempt is safe because the baseline is written AFTER it is read and AFTER the alert decision, so a retried attempt of the same task index sees its own prior write and never re-fires an alert that already went out.

How does one watch list get split across multiple parallel tasks?

Each task reads its own index from CLOUD_RUN_TASK_INDEX and the total task count from CLOUD_RUN_TASK_COUNT, both built-in environment variables Cloud Run sets inside the container. shardFor() keeps every ASIN whose position in the list, modulo the task count, equals this task's own index, so the full list is covered exactly once across all tasks with no overlap.

Do client.js and retry.js need any change to run inside a Cloud Run Job?

No. Cloud Run Jobs run a plain Node.js container, so process.env reads environment variables exactly as the TypeScript quickstart already expects - unlike the Deno (Supabase) or single-flattened-file (Pipedream) adaptations earlier in this series, nothing in client.js or retry.js changes here.

Bottom line: a Cloud Run Job runs this price-watch with zero changes to the TypeScript quickstart's client.js and retry.js, provided the task's own exit code is reserved for a genuine infrastructure failure (never a real SellerMagnet answer), the watch list is sharded by CLOUD_RUN_TASK_INDEX for parallel tasks, and the baseline write order keeps a platform-retried task from re-firing an alert it already sent. That combination of traps is different in shape from every HTTP-triggered platform guide in this series, including the Cloud Functions version on the same Google Cloud. Every endpoint is documented on the API documentation page, more runnable snippets are on the code examples page, and a free account includes 150 credits to try it.

Ready to Extract Amazon Data at Scale?

Start building with SellerMagnet API today. Real-time product data, competitive pricing, and review analytics at your fingertips.

150 free API credits • No credit card required • Cancel anytime