santifer/career-ops · error · Error

APIFY_TOKEN not set

Error message

APIFY_TOKEN not set

What it means

runActor requires an Apify API token, taken by default from the APIFY_TOKEN environment variable. If no token is provided (neither via options nor env), the function throws immediately before making any API call.

Solutions

  1. Add APIFY_TOKEN=<token> to your .env file (get a token from the Apify console account settings).
  2. Pass the token explicitly: runActor(actorId, input, { token }).
  3. Ensure your process actually loads .env (e.g. dotenv/config or the app's env loader) before invoking.
  4. Check for typos in the variable name in .env / CI secrets.

Example fix

// .env before
# (missing)
// after
APIFY_TOKEN=apify_api_XXXXXXXXXXXXXXXX
Defensive patterns

Strategy: validation

Validate before calling

if (!process.env.APIFY_TOKEN) throw new Error('Set APIFY_TOKEN in .env before running actors');

Type guard

const hasApifyToken = () => typeof process.env.APIFY_TOKEN === 'string' && process.env.APIFY_TOKEN.length > 0;

Try / catch

try {
  const items = await runActor(actorId, input);
} catch (e) {
  if (String(e.message).includes('APIFY_TOKEN not set')) {
    console.error('Configure APIFY_TOKEN in .env');
    return [];
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling runActor(actorId, input) without a token option while process.env.APIFY_TOKEN is unset or empty.

Common situations: Developer forgot to add APIFY_TOKEN to .env; CI environment lacking the secret; token variable typo'd (APIFY_TOKEN vs APIFY_TOKEN_KEY); running the plugin before enabling it in config.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of santifer/career-ops@aac998c7ed (2026-09-16). Data as JSON: /api/errors/5cdd949ec5d56403. Report an issue: GitHub.

Appendix: source

Thrown at plugins/apify/_apify.mjs:188

}

async function fetchDatasetItems(runId, token, deadline = null) {
  const url = `${APIFY_API_BASE}/actor-runs/${runId}/dataset/items`;
  const items = await fetchJson(
    url,
    { headers: authHeaders(token) },
    PER_REQUEST_TIMEOUT_MS * 2,
    CONNECT_RETRY_ATTEMPTS,
    deadline,
  );
  if (!Array.isArray(items)) {
    throw new Error(`Apify run ${runId} returned non-array dataset payload`);
  }
  return items;
}

export async function runActor(actorId, input, { timeoutMs = DEFAULT_RUN_TIMEOUT_MS, token = process.env.APIFY_TOKEN } = {}) {
  if (!token) throw new Error('APIFY_TOKEN not set');
  if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
    throw new Error(`apify: invalid timeoutMs ${JSON.stringify(timeoutMs)} (must be a positive finite number of milliseconds)`);
  }
  // Single deadline shared across startRun → waitForRun → fetchDatasetItems so
  // the caller's timeoutMs is the end-to-end ceiling, not just the wait loop.
  const deadline = Date.now() + timeoutMs;
  const runId = await startRun(actorId, input, token, deadline);
  const run = await waitForRun(runId, token, deadline, timeoutMs);
  if (run.status !== 'SUCCEEDED') {
    const reason = run.statusMessage ? `: ${run.statusMessage}` : '';
    throw new Error(`Apify actor ${actorId} finished with status ${run.status}${reason}`);
  }
  return await fetchDatasetItems(runId, token, deadline);
}

View on GitHub (pinned to aac998c7ed)