# hreflang and Canonical Checker: return links, codes, x-default (`examsens.ai/hreflang-canonical-checker`) Actor

Validates the hreflang sets and canonical tags of pages you list or read from a sitemap: language and region codes, self references, x-default, return links from every alternate, and canonical placement and targets.

- **URL**: https://apify.com/examsens.ai/hreflang-canonical-checker.md
- **Developed by:** [Ed Rayyis](https://apify.com/examsens.ai) (community)
- **Categories:** SEO tools
- **Stats:** 2 total users, 1 monthly users, 100.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

from $0.90 / 1,000 url checkeds

This Actor is paid per event. You are not charged for the Apify platform usage, but only a fixed price for specific events.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-event

## What's an Apify Actor?

An Actor is a serverless cloud program that runs on the Apify platform. It has two run modes.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.

Apify vocabulary and the platform model are defined once, in the agent quickstart at https://apify.com/agents.md.

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.

Do not guess an integration path. Every one of them is in the agent quickstart at https://apify.com/agents.md: the Apify MCP server, Agent Skills with the Apify CLI, the JavaScript and Python clients, the REST API, and the account-free path for an agent with no human to sign in. It also carries the rule on stating cost before the first paid run.

For examples already wired to this Actor's own input schema, see the [API](#api) section below.

Each client library has reference documentation the quickstart does not restate: [JavaScript/TypeScript](https://docs.apify.com/api/client/js/docs.md) (`npm install apify-client`) and [Python](https://docs.apify.com/api/client/python/docs.md) (`pip install apify-client`).

# README

## hreflang and Canonical Checker: return links, codes, x-default

Give it a list of pages, a sitemap, or both. For each page it reads the canonical and the hreflang alternates, checks them against the rules Google Search Central publishes, and lists every problem with a plain message, such as `en-UK: 'UK' is not an ISO 3166-1 alpha 2 region code (the United Kingdom is GB)`.

It loads every alternate once and checks that it links back, because Google ignores hreflang links between two pages that do not both point to each other \[1].

### What it checks

**hreflang**, read from `link` elements and from the HTTP `Link` header:

- Each value is a language code in ISO 639-1, optionally followed by a region in ISO 3166-1 alpha 2 or a script in ISO 15924, such as `en`, `en-US` or `zh-Hant` \[1]. `x-default` is accepted. A value with both a script and a region, such as `zh-Hant-TW`, is flagged as a warning, since Google documents one or the other \[1]. A value joined with `_` (`pt_BR`) or a three letter language code (`eng`) is an error, with the fix in the message.
- Alternate URLs are fully qualified, with `http` or `https` \[1].
- The page lists itself \[1].
- An `x-default` alternate is present. Google recommends one as the fallback for visitors whose language matches no version \[1]. Its absence is a warning.
- No language code points to two different URLs.
- Every alternate loads, does not redirect, and lists this page in its own hreflang set (return link) \[1].
- `link` elements placed in the body are flagged.

**Canonical**, read from the head, the body and the HTTP `Link` header:

- A canonical is present.
- Only one canonical URL is given. Google asks you not to give different canonical URLs for the same page \[2].
- It is in the head. Google accepts `rel="canonical"` only in the head \[2].
- It is an absolute URL, as Google recommends \[2].
- Its target loads and does not redirect.
- On a page with hreflang, it does not point to a version in another language. Google asks for a canonical in the same language \[2].

Errors and warnings are this actor's grading. Google's pages describe the rules, not a severity.

### Input

- Pages (optional when you give a sitemap)
- Sitemaps (optional): plain or gzipped XML, sitemap index files followed
- Maximum pages (default 200)
- Fetch alternates and canonical targets (default on). Off checks each page's own tags only.
- Maximum extra fetches (default 1,000): alternates and canonical targets loaded beyond the pages being checked. Each URL is loaded once per run.
- Timeout per request (default 20 seconds)

### Output

Dataset: one item per page with `url`, `ok`, `finalUrl`, `httpStatus`, `redirected`, `hreflangCount`, `selfHreflang`, `hasXDefault`, `hreflang` (each with `hreflang`, `href`, `where`, `validCode`, `targetStatus`, `returnLink`), `canonical`, `canonicalCount`, `canonicalIsSelf`, `canonicalStatus`, `issues` (each with `severity`, `code` and `message`), `issueCodes`, `errorCount` and `warningCount`.

The key value store holds a `SUMMARY` record with the pages checked, the pages with errors, and any sitemap that could not be read. A page that cannot be loaded gets `ok: false` and an `error`, and the rest of the run carries on. Export as JSON, CSV or Excel, or read it over the API.

hreflang annotations inside XML sitemaps are not checked. The actor reads sitemaps only for their page list.

### Pricing

Pay per event: one event per page checked. Alternates and canonical targets loaded for the return link checks are not charged. A page that cannot be loaded is not charged. The run stops when it reaches the maximum charge you set.

### Scope and conduct

Only public pages are loaded, with at most five requests at a time. No browser is used, nothing is logged in to, and no personal data is collected.

### Support

Questions and problems: the Issues tab on this page. Replies are written by an AI assistant on behalf of the developer and reviewed by the developer.

### References

\[1] Google Search Central, Localized Versions of your Pages. https://developers.google.com/search/docs/specialty/international/localized-versions

\[2] Google Search Central, How to Specify a Canonical with rel="canonical" and Other Methods. https://developers.google.com/search/docs/crawling-indexing/consolidate-duplicate-urls

# Actor input Schema

## `urls` (type: `array`):

Pages to check. You can leave this empty and give a sitemap below instead, or use both.

## `sitemapUrls` (type: `array`):

XML sitemap URLs (plain or gzipped, sitemap index files are followed). Every page they list is checked, up to the maximum below.

## `maxUrls` (type: `integer`):

Most pages to check in one run, across the list and the sitemaps. Each page checked is one billable event.

## `checkAlternates` (type: `boolean`):

Load every hreflang alternate and canonical target once to check its status and whether it links back. When off, only each page's own tags are checked.

## `maxExtraFetches` (type: `integer`):

Most alternate and canonical target pages loaded in one run, beyond the pages being checked. Alternates already in your list are loaded only once.

## `timeoutSecs` (type: `integer`):

Give up on one page after this many seconds.

## Actor input object example

```json
{
  "urls": [
    {
      "url": "https://www.apple.com/"
    }
  ],
  "maxUrls": 200,
  "checkAlternates": true,
  "maxExtraFetches": 1000,
  "timeoutSecs": 20
}
```

# Actor output Schema

## `results` (type: `string`):

No description

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "urls": [
        {
            "url": "https://www.apple.com/"
        }
    ]
};

// Run the Actor and wait for it to finish
const run = await client.actor("examsens.ai/hreflang-canonical-checker").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = { "urls": [{ "url": "https://www.apple.com/" }] }

# Run the Actor and wait for it to finish
run = client.actor("examsens.ai/hreflang-canonical-checker").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "urls": [
    {
      "url": "https://www.apple.com/"
    }
  ]
}' |
apify call examsens.ai/hreflang-canonical-checker --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,examsens.ai/hreflang-canonical-checker"
        }
    }
}
```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/cf8CmZFjVu0WIVynV/builds/PdqV1ri08tZsqgN7K/openapi.json
