semrush-api-client 1.0.11 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +50 -0
  2. package/dist/client/index.d.ts +14 -0
  3. package/dist/client/index.js +34 -0
  4. package/dist/index.d.ts +1 -0
  5. package/dist/index.js +1 -0
  6. package/dist/v4/backlinks/getAnchorsV4.d.ts +5 -0
  7. package/dist/v4/backlinks/getAnchorsV4.js +11 -0
  8. package/dist/v4/backlinks/getBacklinksCompetitorsV4.d.ts +12 -0
  9. package/dist/v4/backlinks/getBacklinksCompetitorsV4.js +18 -0
  10. package/dist/v4/backlinks/getBacklinksHistoricalSummaryV4.d.ts +12 -0
  11. package/dist/v4/backlinks/getBacklinksHistoricalSummaryV4.js +17 -0
  12. package/dist/v4/backlinks/getBacklinksOverviewV4.d.ts +9 -0
  13. package/dist/v4/backlinks/getBacklinksOverviewV4.js +14 -0
  14. package/dist/v4/backlinks/getBacklinksScoreProfileV4.d.ts +8 -0
  15. package/dist/v4/backlinks/getBacklinksScoreProfileV4.js +13 -0
  16. package/dist/v4/backlinks/getBacklinksV4.d.ts +5 -0
  17. package/dist/v4/backlinks/getBacklinksV4.js +10 -0
  18. package/dist/v4/backlinks/getIndexedPagesV4.d.ts +5 -0
  19. package/dist/v4/backlinks/getIndexedPagesV4.js +11 -0
  20. package/dist/v4/backlinks/getReferringDomainsV4.d.ts +5 -0
  21. package/dist/v4/backlinks/getReferringDomainsV4.js +10 -0
  22. package/dist/v4/backlinks/getReferringIpsV4.d.ts +5 -0
  23. package/dist/v4/backlinks/getReferringIpsV4.js +10 -0
  24. package/dist/v4/backlinks/index.d.ts +11 -0
  25. package/dist/v4/backlinks/index.js +36 -0
  26. package/dist/v4/backlinks/listParams.d.ts +14 -0
  27. package/dist/v4/backlinks/listParams.js +16 -0
  28. package/dist/v4/backlinks/types.d.ts +95 -0
  29. package/dist/v4/backlinks/types.js +4 -0
  30. package/dist/v4/index.d.ts +3 -0
  31. package/dist/v4/index.js +19 -0
  32. package/dist/v4/keyword/getKeywordMetricsV4.d.ts +19 -0
  33. package/dist/v4/keyword/getKeywordMetricsV4.js +21 -0
  34. package/dist/v4/keyword/index.d.ts +2 -0
  35. package/dist/v4/keyword/index.js +5 -0
  36. package/dist/v4/types.d.ts +20 -0
  37. package/dist/v4/types.js +5 -0
  38. package/package.json +5 -2
package/README.md CHANGED
@@ -34,6 +34,56 @@ client.getDomainOverview({ domain: "apple.com" });
34
34
  client.getKeywordOverview({ phrase: "apple" });
35
35
  ```
36
36
 
37
+ ## API v4
38
+
39
+ Semrush's v4 endpoints are also available. They are suffixed with `V4`, take an
40
+ `Authorization: Apikey` header instead of a `key` query param, and return a JSON
41
+ `{ meta, data }` envelope instead of CSV rows.
42
+
43
+ ```ts
44
+ // Keyword Reports v4 (replaces the deprecated v3 keyword reports)
45
+ const { data } = await client.getKeywordMetricsV4({
46
+ keyword: "seo tools",
47
+ country: "us", // ISO 3166-1 alpha-2
48
+ // month: "2026-06", // Optional, defaults to the current month
49
+ });
50
+ console.log(data.search_volume, data.keyword_difficulty, data.intents);
51
+
52
+ // Backlinks v4
53
+ await client.getBacklinksOverviewV4({ url: "apple.com", scope: "ROOT_DOMAIN" });
54
+ await client.getBacklinksV4({ url: "apple.com", scope: "ROOT_DOMAIN", limit: 10 });
55
+ ```
56
+
57
+ | Method | Endpoint |
58
+ | --- | --- |
59
+ | `getKeywordMetricsV4` | `/apis/v4/keywords/v1/metrics` |
60
+ | `getBacklinksOverviewV4` | `/apis/v4/backlinks/v1/overview` |
61
+ | `getBacklinksHistoricalSummaryV4` | `/apis/v4/backlinks/v1/summary` |
62
+ | `getBacklinksV4` | `/apis/v4/backlinks/v1/links` |
63
+ | `getReferringDomainsV4` | `/apis/v4/backlinks/v1/ref-domains` |
64
+ | `getReferringIpsV4` | `/apis/v4/backlinks/v1/ref-ips` |
65
+ | `getIndexedPagesV4` | `/apis/v4/backlinks/v1/pages` |
66
+ | `getAnchorsV4` | `/apis/v4/backlinks/v1/anchors` |
67
+ | `getBacklinksScoreProfileV4` | `/apis/v4/backlinks/v1/score-profile` |
68
+ | `getBacklinksCompetitorsV4` | `/apis/v4/backlinks/v1/competitors` |
69
+
70
+ `scope` accepts `ROOT_DOMAIN`, `SUBDOMAIN`, `SUBFOLDER` or `PAGE` (it replaces
71
+ v3's `target_type`). The list reports take `fields`, `orderBy`, `direction`,
72
+ `limit`, `offset` and `filter`.
73
+
74
+ > **Note:** v4 access is granted separately from v3. A key that works for the v3
75
+ > reports can still return `401 Unauthorized` on v4 — check that your Semrush
76
+ > plan and API key are enabled for API v4.
77
+
78
+ ## Running the endpoint smoke tests
79
+
80
+ ```sh
81
+ npm run test:endpoints # v3
82
+ npm run test:endpoints:v4 # v4
83
+ ```
84
+
85
+ Both read `SEMRUSH_API_KEY` from `.env` and consume real API units.
86
+
37
87
  ## Contributing
38
88
 
39
89
  Contributions are welcome! Feel free to open issues or submit pull requests to improve this project.
@@ -5,16 +5,20 @@ import { getRankDifference, getSemrushRank } from "../overview";
5
5
  import { getTrafficSummary } from "../trend";
6
6
  import { getUrlOverview, getUrlOrganicSearchKeywords, getUrlPaidSearchKeywords } from "../url";
7
7
  import { countApiUnits } from "../user";
8
+ import { getBacklinksOverviewV4, getBacklinksHistoricalSummaryV4, getBacklinksV4, getReferringDomainsV4, getReferringIpsV4, getIndexedPagesV4, getAnchorsV4, getBacklinksScoreProfileV4, getBacklinksCompetitorsV4, getKeywordMetricsV4 } from "../v4";
9
+ import { V4Response } from "../v4/types";
8
10
  export declare class SemrushAPIClient {
9
11
  private client;
10
12
  private api_key;
11
13
  BASE_URL: string;
12
14
  ANALYTICS_URL: string;
15
+ V4_URL: string;
13
16
  constructor({ api_key }: {
14
17
  api_key: string;
15
18
  });
16
19
  parseCsvToObjects(csvString: string, delimiter?: string): Record<string, string>[];
17
20
  get<T>(url: string, params?: Record<string, any>, outputObj?: boolean): Promise<T>;
21
+ getV4<T>(path: string, params?: Record<string, unknown>): Promise<V4Response<T>>;
18
22
  getDomainOverview: typeof getDomainOverview;
19
23
  getDomainOverviewOneDatabase: typeof getDomainOverviewOneDatabase;
20
24
  getDomainOverviewHistory: typeof getDomainOverviewHistory;
@@ -42,4 +46,14 @@ export declare class SemrushAPIClient {
42
46
  getUrlOrganicSearchKeywords: typeof getUrlOrganicSearchKeywords;
43
47
  getUrlPaidSearchKeywords: typeof getUrlPaidSearchKeywords;
44
48
  countApiUnits: typeof countApiUnits;
49
+ getBacklinksOverviewV4: typeof getBacklinksOverviewV4;
50
+ getBacklinksHistoricalSummaryV4: typeof getBacklinksHistoricalSummaryV4;
51
+ getBacklinksV4: typeof getBacklinksV4;
52
+ getReferringDomainsV4: typeof getReferringDomainsV4;
53
+ getReferringIpsV4: typeof getReferringIpsV4;
54
+ getIndexedPagesV4: typeof getIndexedPagesV4;
55
+ getAnchorsV4: typeof getAnchorsV4;
56
+ getBacklinksScoreProfileV4: typeof getBacklinksScoreProfileV4;
57
+ getBacklinksCompetitorsV4: typeof getBacklinksCompetitorsV4;
58
+ getKeywordMetricsV4: typeof getKeywordMetricsV4;
45
59
  }
@@ -12,11 +12,13 @@ const overview_1 = require("../overview");
12
12
  const trend_1 = require("../trend");
13
13
  const url_1 = require("../url");
14
14
  const user_1 = require("../user");
15
+ const v4_1 = require("../v4");
15
16
  class SemrushAPIClient {
16
17
  client;
17
18
  api_key;
18
19
  BASE_URL = "https://api.semrush.com/";
19
20
  ANALYTICS_URL = "https://api.semrush.com/analytics/";
21
+ V4_URL = "https://api.semrush.com/apis/v4/";
20
22
  constructor({ api_key }) {
21
23
  this.api_key = api_key;
22
24
  this.client = axios_1.default.create({
@@ -57,6 +59,26 @@ class SemrushAPIClient {
57
59
  }
58
60
  }
59
61
  }
62
+ // API v4 request: the key travels in the Authorization header and the
63
+ // response is a JSON { meta, data } envelope instead of v3's CSV body.
64
+ async getV4(path, params) {
65
+ try {
66
+ const response = await this.client.get(this.V4_URL + path, {
67
+ params,
68
+ headers: { Authorization: `Apikey ${this.api_key}` },
69
+ });
70
+ return response.data;
71
+ }
72
+ catch (error) {
73
+ if (axios_1.default.isAxiosError(error)) {
74
+ const body = error.response?.data;
75
+ const apiMessage = body?.error?.message;
76
+ const requestId = body?.meta?.request_id;
77
+ throw new Error(`Semrush v4 request failed (${path}): ${apiMessage ?? error.message}${error.response?.status ? ` (status: ${error.response.status})` : ""}${requestId ? ` (request_id: ${requestId})` : ""}`);
78
+ }
79
+ throw new Error(`Unexpected error: ${error}`);
80
+ }
81
+ }
60
82
  // Domain methods
61
83
  getDomainOverview = domain_1.getDomainOverview;
62
84
  getDomainOverviewOneDatabase = domain_1.getDomainOverviewOneDatabase;
@@ -91,5 +113,17 @@ class SemrushAPIClient {
91
113
  getUrlPaidSearchKeywords = url_1.getUrlPaidSearchKeywords;
92
114
  // User methods
93
115
  countApiUnits = user_1.countApiUnits;
116
+ // Backlinks methods (API v4)
117
+ getBacklinksOverviewV4 = v4_1.getBacklinksOverviewV4;
118
+ getBacklinksHistoricalSummaryV4 = v4_1.getBacklinksHistoricalSummaryV4;
119
+ getBacklinksV4 = v4_1.getBacklinksV4;
120
+ getReferringDomainsV4 = v4_1.getReferringDomainsV4;
121
+ getReferringIpsV4 = v4_1.getReferringIpsV4;
122
+ getIndexedPagesV4 = v4_1.getIndexedPagesV4;
123
+ getAnchorsV4 = v4_1.getAnchorsV4;
124
+ getBacklinksScoreProfileV4 = v4_1.getBacklinksScoreProfileV4;
125
+ getBacklinksCompetitorsV4 = v4_1.getBacklinksCompetitorsV4;
126
+ // Keyword methods (API v4)
127
+ getKeywordMetricsV4 = v4_1.getKeywordMetricsV4;
94
128
  }
95
129
  exports.SemrushAPIClient = SemrushAPIClient;
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./client";
2
2
  export { Database } from "./type/general/database";
3
+ export * from "./v4";
package/dist/index.js CHANGED
@@ -20,3 +20,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
20
20
  const dotenv_1 = __importDefault(require("dotenv"));
21
21
  dotenv_1.default.config();
22
22
  __exportStar(require("./client"), exports);
23
+ __exportStar(require("./v4"), exports);
@@ -0,0 +1,5 @@
1
+ import { SemrushAPIClient } from "../../client";
2
+ import { V4Response } from "../types";
3
+ import { BacklinksListArgs } from "./listParams";
4
+ import { AnchorField, AnchorRow } from "./types";
5
+ export declare function getAnchorsV4(this: SemrushAPIClient, args: BacklinksListArgs<AnchorField>): Promise<V4Response<AnchorRow[]>>;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAnchorsV4 = getAnchorsV4;
4
+ const listParams_1 = require("./listParams");
5
+ // GET /apis/v4/backlinks/v1/anchors
6
+ // Anchor texts used by the backlinks. v4 replacement for
7
+ // type=backlinks_anchors. Defaults: order_by=domains_count, direction=DESC,
8
+ // limit=100.
9
+ async function getAnchorsV4(args) {
10
+ return this.getV4("backlinks/v1/anchors", (0, listParams_1.buildBacklinksListParams)(args));
11
+ }
@@ -0,0 +1,12 @@
1
+ import { SemrushAPIClient } from "../../client";
2
+ import { V4Direction, V4Format, V4Response } from "../types";
3
+ import { CompetitorField, CompetitorRow } from "./types";
4
+ export declare function getBacklinksCompetitorsV4(this: SemrushAPIClient, { domain, fields, orderBy, direction, limit, offset, format, }: {
5
+ domain: string;
6
+ fields?: CompetitorField[];
7
+ orderBy?: CompetitorField;
8
+ direction?: V4Direction;
9
+ limit?: number;
10
+ offset?: number;
11
+ format?: V4Format;
12
+ }): Promise<V4Response<CompetitorRow[]>>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBacklinksCompetitorsV4 = getBacklinksCompetitorsV4;
4
+ // GET /apis/v4/backlinks/v1/competitors
5
+ // Domains with a backlink profile similar to the target. Unlike the other
6
+ // Backlinks v4 reports this one takes `domain`, not `url`/`scope`.
7
+ // Defaults: order_by=similarity, direction=DESC, limit=100.
8
+ async function getBacklinksCompetitorsV4({ domain, fields, orderBy, direction, limit, offset, format, }) {
9
+ return this.getV4("backlinks/v1/competitors", {
10
+ domain,
11
+ fields: fields?.join(","),
12
+ order_by: orderBy,
13
+ direction,
14
+ limit,
15
+ offset,
16
+ format,
17
+ });
18
+ }
@@ -0,0 +1,12 @@
1
+ import { SemrushAPIClient } from "../../client";
2
+ import { V4Format, V4Response } from "../types";
3
+ import { BacklinksScope, BacklinksSummaryField, BacklinksSummaryRow } from "./types";
4
+ export declare function getBacklinksHistoricalSummaryV4(this: SemrushAPIClient, { url, scope, fields, limit, dateFrom, dateTo, format, }: {
5
+ url: string;
6
+ scope?: BacklinksScope;
7
+ fields?: BacklinksSummaryField[];
8
+ limit?: number;
9
+ dateFrom?: string;
10
+ dateTo?: string;
11
+ format?: V4Format;
12
+ }): Promise<V4Response<BacklinksSummaryRow[]>>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBacklinksHistoricalSummaryV4 = getBacklinksHistoricalSummaryV4;
4
+ // GET /apis/v4/backlinks/v1/summary
5
+ // Month-by-month history of the headline backlink metrics. No v3 equivalent in
6
+ // this client.
7
+ async function getBacklinksHistoricalSummaryV4({ url, scope = "ROOT_DOMAIN", fields, limit, dateFrom, dateTo, format, }) {
8
+ return this.getV4("backlinks/v1/summary", {
9
+ url,
10
+ scope,
11
+ fields: fields?.join(","),
12
+ limit,
13
+ date_from: dateFrom,
14
+ date_to: dateTo,
15
+ format,
16
+ });
17
+ }
@@ -0,0 +1,9 @@
1
+ import { SemrushAPIClient } from "../../client";
2
+ import { V4Format, V4Response } from "../types";
3
+ import { BacklinksOverviewData, BacklinksOverviewField, BacklinksScope } from "./types";
4
+ export declare function getBacklinksOverviewV4(this: SemrushAPIClient, { url, scope, fields, format, }: {
5
+ url: string;
6
+ scope?: BacklinksScope;
7
+ fields?: BacklinksOverviewField[];
8
+ format?: V4Format;
9
+ }): Promise<V4Response<BacklinksOverviewData>>;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBacklinksOverviewV4 = getBacklinksOverviewV4;
4
+ // GET /apis/v4/backlinks/v1/overview
5
+ // Current backlink totals for a target. v4 replacement for
6
+ // type=backlinks_overview.
7
+ async function getBacklinksOverviewV4({ url, scope = "ROOT_DOMAIN", fields, format, }) {
8
+ return this.getV4("backlinks/v1/overview", {
9
+ url,
10
+ scope,
11
+ fields: fields?.join(","),
12
+ format,
13
+ });
14
+ }
@@ -0,0 +1,8 @@
1
+ import { SemrushAPIClient } from "../../client";
2
+ import { V4Format, V4Response } from "../types";
3
+ import { BacklinksScope, ScoreProfileData } from "./types";
4
+ export declare function getBacklinksScoreProfileV4(this: SemrushAPIClient, { url, scope, format, }: {
5
+ url: string;
6
+ scope?: BacklinksScope;
7
+ format?: V4Format;
8
+ }): Promise<V4Response<ScoreProfileData[]>>;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBacklinksScoreProfileV4 = getBacklinksScoreProfileV4;
4
+ // GET /apis/v4/backlinks/v1/score-profile
5
+ // Distribution of referring domains by Authority Score. No v3 equivalent in
6
+ // this client. This endpoint takes no `fields` parameter.
7
+ async function getBacklinksScoreProfileV4({ url, scope = "ROOT_DOMAIN", format, }) {
8
+ return this.getV4("backlinks/v1/score-profile", {
9
+ url,
10
+ scope,
11
+ format,
12
+ });
13
+ }
@@ -0,0 +1,5 @@
1
+ import { SemrushAPIClient } from "../../client";
2
+ import { V4Response } from "../types";
3
+ import { BacklinksListArgs } from "./listParams";
4
+ import { BacklinkField, BacklinkRow } from "./types";
5
+ export declare function getBacklinksV4(this: SemrushAPIClient, args: BacklinksListArgs<BacklinkField>): Promise<V4Response<BacklinkRow[]>>;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBacklinksV4 = getBacklinksV4;
4
+ const listParams_1 = require("./listParams");
5
+ // GET /apis/v4/backlinks/v1/links
6
+ // Individual backlinks pointing at the target. v4 replacement for
7
+ // type=backlinks. Defaults: order_by=page_score, direction=DESC, limit=100.
8
+ async function getBacklinksV4(args) {
9
+ return this.getV4("backlinks/v1/links", (0, listParams_1.buildBacklinksListParams)(args));
10
+ }
@@ -0,0 +1,5 @@
1
+ import { SemrushAPIClient } from "../../client";
2
+ import { V4Response } from "../types";
3
+ import { BacklinksListArgs } from "./listParams";
4
+ import { IndexedPageField, IndexedPageRow } from "./types";
5
+ export declare function getIndexedPagesV4(this: SemrushAPIClient, args: BacklinksListArgs<IndexedPageField>): Promise<V4Response<IndexedPageRow[]>>;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getIndexedPagesV4 = getIndexedPagesV4;
4
+ const listParams_1 = require("./listParams");
5
+ // GET /apis/v4/backlinks/v1/pages
6
+ // Pages of the target that hold backlinks. v4 replacement for
7
+ // type=backlinks_pages. Defaults: order_by=domains_count, direction=DESC,
8
+ // limit=100.
9
+ async function getIndexedPagesV4(args) {
10
+ return this.getV4("backlinks/v1/pages", (0, listParams_1.buildBacklinksListParams)(args));
11
+ }
@@ -0,0 +1,5 @@
1
+ import { SemrushAPIClient } from "../../client";
2
+ import { V4Response } from "../types";
3
+ import { BacklinksListArgs } from "./listParams";
4
+ import { ReferringDomainField, ReferringDomainRow } from "./types";
5
+ export declare function getReferringDomainsV4(this: SemrushAPIClient, args: BacklinksListArgs<ReferringDomainField>): Promise<V4Response<ReferringDomainRow[]>>;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getReferringDomainsV4 = getReferringDomainsV4;
4
+ const listParams_1 = require("./listParams");
5
+ // GET /apis/v4/backlinks/v1/ref-domains
6
+ // Domains linking to the target. v4 replacement for type=backlinks_refdomains.
7
+ // Defaults: order_by=backlinks_count, direction=DESC, limit=100.
8
+ async function getReferringDomainsV4(args) {
9
+ return this.getV4("backlinks/v1/ref-domains", (0, listParams_1.buildBacklinksListParams)(args));
10
+ }
@@ -0,0 +1,5 @@
1
+ import { SemrushAPIClient } from "../../client";
2
+ import { V4Response } from "../types";
3
+ import { BacklinksListArgs } from "./listParams";
4
+ import { ReferringIpField, ReferringIpRow } from "./types";
5
+ export declare function getReferringIpsV4(this: SemrushAPIClient, args: BacklinksListArgs<ReferringIpField>): Promise<V4Response<ReferringIpRow[]>>;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getReferringIpsV4 = getReferringIpsV4;
4
+ const listParams_1 = require("./listParams");
5
+ // GET /apis/v4/backlinks/v1/ref-ips
6
+ // IP addresses hosting the referring domains. No v3 equivalent in this client.
7
+ // Defaults: order_by=domains_count, direction=DESC, limit=100.
8
+ async function getReferringIpsV4(args) {
9
+ return this.getV4("backlinks/v1/ref-ips", (0, listParams_1.buildBacklinksListParams)(args));
10
+ }
@@ -0,0 +1,11 @@
1
+ export { getBacklinksOverviewV4 } from "./getBacklinksOverviewV4";
2
+ export { getBacklinksHistoricalSummaryV4 } from "./getBacklinksHistoricalSummaryV4";
3
+ export { getBacklinksV4 } from "./getBacklinksV4";
4
+ export { getReferringDomainsV4 } from "./getReferringDomainsV4";
5
+ export { getReferringIpsV4 } from "./getReferringIpsV4";
6
+ export { getIndexedPagesV4 } from "./getIndexedPagesV4";
7
+ export { getAnchorsV4 } from "./getAnchorsV4";
8
+ export { getBacklinksScoreProfileV4 } from "./getBacklinksScoreProfileV4";
9
+ export { getBacklinksCompetitorsV4 } from "./getBacklinksCompetitorsV4";
10
+ export * from "./types";
11
+ export type { BacklinksListArgs } from "./listParams";
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.getBacklinksCompetitorsV4 = exports.getBacklinksScoreProfileV4 = exports.getAnchorsV4 = exports.getIndexedPagesV4 = exports.getReferringIpsV4 = exports.getReferringDomainsV4 = exports.getBacklinksV4 = exports.getBacklinksHistoricalSummaryV4 = exports.getBacklinksOverviewV4 = void 0;
18
+ var getBacklinksOverviewV4_1 = require("./getBacklinksOverviewV4");
19
+ Object.defineProperty(exports, "getBacklinksOverviewV4", { enumerable: true, get: function () { return getBacklinksOverviewV4_1.getBacklinksOverviewV4; } });
20
+ var getBacklinksHistoricalSummaryV4_1 = require("./getBacklinksHistoricalSummaryV4");
21
+ Object.defineProperty(exports, "getBacklinksHistoricalSummaryV4", { enumerable: true, get: function () { return getBacklinksHistoricalSummaryV4_1.getBacklinksHistoricalSummaryV4; } });
22
+ var getBacklinksV4_1 = require("./getBacklinksV4");
23
+ Object.defineProperty(exports, "getBacklinksV4", { enumerable: true, get: function () { return getBacklinksV4_1.getBacklinksV4; } });
24
+ var getReferringDomainsV4_1 = require("./getReferringDomainsV4");
25
+ Object.defineProperty(exports, "getReferringDomainsV4", { enumerable: true, get: function () { return getReferringDomainsV4_1.getReferringDomainsV4; } });
26
+ var getReferringIpsV4_1 = require("./getReferringIpsV4");
27
+ Object.defineProperty(exports, "getReferringIpsV4", { enumerable: true, get: function () { return getReferringIpsV4_1.getReferringIpsV4; } });
28
+ var getIndexedPagesV4_1 = require("./getIndexedPagesV4");
29
+ Object.defineProperty(exports, "getIndexedPagesV4", { enumerable: true, get: function () { return getIndexedPagesV4_1.getIndexedPagesV4; } });
30
+ var getAnchorsV4_1 = require("./getAnchorsV4");
31
+ Object.defineProperty(exports, "getAnchorsV4", { enumerable: true, get: function () { return getAnchorsV4_1.getAnchorsV4; } });
32
+ var getBacklinksScoreProfileV4_1 = require("./getBacklinksScoreProfileV4");
33
+ Object.defineProperty(exports, "getBacklinksScoreProfileV4", { enumerable: true, get: function () { return getBacklinksScoreProfileV4_1.getBacklinksScoreProfileV4; } });
34
+ var getBacklinksCompetitorsV4_1 = require("./getBacklinksCompetitorsV4");
35
+ Object.defineProperty(exports, "getBacklinksCompetitorsV4", { enumerable: true, get: function () { return getBacklinksCompetitorsV4_1.getBacklinksCompetitorsV4; } });
36
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,14 @@
1
+ import { V4Direction, V4Format } from "../types";
2
+ import { BacklinksScope } from "./types";
3
+ export type BacklinksListArgs<TField extends string> = {
4
+ url: string;
5
+ scope: BacklinksScope;
6
+ fields?: TField[];
7
+ orderBy?: TField;
8
+ direction?: V4Direction;
9
+ limit?: number;
10
+ offset?: number;
11
+ filter?: string;
12
+ format?: V4Format;
13
+ };
14
+ export declare function buildBacklinksListParams<TField extends string>({ url, scope, fields, orderBy, direction, limit, offset, filter, format, }: BacklinksListArgs<TField>): Record<string, unknown>;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildBacklinksListParams = buildBacklinksListParams;
4
+ function buildBacklinksListParams({ url, scope, fields, orderBy, direction, limit, offset, filter, format, }) {
5
+ return {
6
+ url,
7
+ scope,
8
+ fields: fields?.join(","),
9
+ order_by: orderBy,
10
+ direction,
11
+ limit,
12
+ offset,
13
+ filter,
14
+ format,
15
+ };
16
+ }
@@ -0,0 +1,95 @@
1
+ export type BacklinksScope = "ROOT_DOMAIN" | "SUBDOMAIN" | "SUBFOLDER" | "PAGE";
2
+ export type BacklinksOverviewField = "backlinks_count" | "domains_count" | "follows_count" | "forms_count" | "frames_count" | "images_count" | "ip_class_c_count" | "ips_count" | "lost_count" | "new_count" | "nofollows_count" | "score" | "sponsored_count" | "texts_count" | "ugc_count" | "urls_count";
3
+ export type BacklinksOverviewData = Partial<Record<BacklinksOverviewField, number>>;
4
+ export type BacklinksSummaryField = "backlinks_count" | "domains_count" | "follows_count" | "month_date" | "score";
5
+ export type BacklinksSummaryRow = {
6
+ backlinks_count?: number;
7
+ domains_count?: number;
8
+ follows_count?: number;
9
+ month_date?: string;
10
+ score?: number;
11
+ url?: string;
12
+ };
13
+ export type BacklinkField = "anchor" | "domain_score" | "external_links_count" | "first_seen_at" | "image_alt" | "image_url" | "internal_links_count" | "ip_address" | "is_form" | "is_frame" | "is_image" | "is_lost" | "is_new" | "is_nofollow" | "is_sitewide_main" | "is_sponsored" | "is_ugc" | "lang" | "last_seen_at" | "page_score" | "redirect_url" | "response_code" | "source_domain" | "source_size" | "source_title" | "source_url" | "target_title" | "target_url";
14
+ export type BacklinkRow = {
15
+ anchor?: string;
16
+ domain_score?: number;
17
+ external_links_count?: number;
18
+ first_seen_at?: string;
19
+ image_alt?: string;
20
+ image_url?: string;
21
+ internal_links_count?: number;
22
+ ip_address?: string;
23
+ is_form?: boolean;
24
+ is_frame?: boolean;
25
+ is_image?: boolean;
26
+ is_lost?: boolean;
27
+ is_new?: boolean;
28
+ is_nofollow?: boolean;
29
+ is_sitewide_main?: boolean;
30
+ is_sponsored?: boolean;
31
+ is_ugc?: boolean;
32
+ lang?: string;
33
+ last_seen_at?: string;
34
+ page_score?: number;
35
+ redirect_url?: string;
36
+ response_code?: number;
37
+ source_domain?: string;
38
+ source_size?: number;
39
+ source_title?: string;
40
+ source_url?: string;
41
+ target_title?: string;
42
+ target_url?: string;
43
+ };
44
+ export type ReferringDomainField = "backlinks_count" | "country" | "domain" | "domain_score" | "first_seen_at" | "ip_address" | "is_follow" | "is_lost" | "is_new" | "last_seen_at";
45
+ export type ReferringDomainRow = Partial<{
46
+ backlinks_count: number;
47
+ country: string;
48
+ domain: string;
49
+ domain_score: number;
50
+ first_seen_at: string;
51
+ ip_address: string;
52
+ is_follow: boolean;
53
+ is_lost: boolean;
54
+ is_new: boolean;
55
+ last_seen_at: string;
56
+ }>;
57
+ export type ReferringIpField = "backlinks_count" | "country" | "domains_count" | "first_seen_at" | "ip_address" | "last_seen_at";
58
+ export type ReferringIpRow = Partial<{
59
+ backlinks_count: number;
60
+ country: string;
61
+ domains_count: number;
62
+ first_seen_at: string;
63
+ ip_address: string;
64
+ last_seen_at: string;
65
+ }>;
66
+ export type IndexedPageField = "backlinks_count" | "domains_count" | "external_links_count" | "first_seen_at" | "internal_links_count" | "last_seen_at" | "response_code" | "source_title" | "source_url";
67
+ export type IndexedPageRow = Partial<{
68
+ backlinks_count: number;
69
+ domains_count: number;
70
+ external_links_count: number;
71
+ first_seen_at: string;
72
+ internal_links_count: number;
73
+ last_seen_at: string;
74
+ response_code: number;
75
+ source_title: string;
76
+ source_url: string;
77
+ }>;
78
+ export type AnchorField = "anchor" | "backlinks_count" | "domains_count" | "first_seen_at" | "last_seen_at";
79
+ export type AnchorRow = Partial<{
80
+ anchor: string;
81
+ backlinks_count: number;
82
+ domains_count: number;
83
+ first_seen_at: string;
84
+ last_seen_at: string;
85
+ }>;
86
+ export type ScoreProfileData = Partial<{
87
+ domain_score: number;
88
+ domains_count: number;
89
+ }>;
90
+ export type CompetitorField = "common_refdomains" | "domain" | "similarity";
91
+ export type CompetitorRow = Partial<{
92
+ common_refdomains: number;
93
+ domain: string;
94
+ similarity: number;
95
+ }>;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ // Types shared by the v4 Backlinks endpoints
3
+ // (https://developer.semrush.com/api/v4/seo/backlinks/).
4
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export * from "./types";
2
+ export * from "./backlinks";
3
+ export * from "./keyword";
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
18
+ __exportStar(require("./backlinks"), exports);
19
+ __exportStar(require("./keyword"), exports);
@@ -0,0 +1,19 @@
1
+ import { SemrushAPIClient } from "../../client";
2
+ import { V4Format, V4Response } from "../types";
3
+ export type KeywordIntent = "COMMERCIAL" | "INFORMATIONAL" | "NAVIGATIONAL" | "TRANSACTIONAL";
4
+ export type KeywordMetricsData = {
5
+ competitive_density?: number;
6
+ cpc?: string;
7
+ intents?: KeywordIntent[];
8
+ keyword_difficulty?: number;
9
+ number_of_results?: string;
10
+ search_volume?: string;
11
+ serp_features?: string[];
12
+ trends?: number[];
13
+ };
14
+ export declare function getKeywordMetricsV4(this: SemrushAPIClient, { keyword, country, month, format, }: {
15
+ keyword: string;
16
+ country: string;
17
+ month?: string;
18
+ format?: V4Format;
19
+ }): Promise<V4Response<KeywordMetricsData>>;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getKeywordMetricsV4 = getKeywordMetricsV4;
4
+ // GET /apis/v4/keywords/v1/metrics
5
+ // Volume, difficulty, CPC, intents, SERP features and trend for one keyword.
6
+ // v4 replacement for the deprecated v3 phrase_this / phrase_kdi reports.
7
+ // Price: 20 API units per request.
8
+ async function getKeywordMetricsV4({ keyword, country, month, format, }) {
9
+ if (keyword.length < 1 || keyword.length > 255) {
10
+ throw new Error("keyword must be between 1 and 255 characters.");
11
+ }
12
+ if (month && !/^\d{4}-\d{2}$/.test(month)) {
13
+ throw new Error("Invalid month format. Format: YYYY-MM");
14
+ }
15
+ return this.getV4("keywords/v1/metrics", {
16
+ keyword,
17
+ country,
18
+ month,
19
+ format,
20
+ });
21
+ }
@@ -0,0 +1,2 @@
1
+ export { getKeywordMetricsV4 } from "./getKeywordMetricsV4";
2
+ export type { KeywordIntent, KeywordMetricsData, } from "./getKeywordMetricsV4";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getKeywordMetricsV4 = void 0;
4
+ var getKeywordMetricsV4_1 = require("./getKeywordMetricsV4");
5
+ Object.defineProperty(exports, "getKeywordMetricsV4", { enumerable: true, get: function () { return getKeywordMetricsV4_1.getKeywordMetricsV4; } });
@@ -0,0 +1,20 @@
1
+ export type V4Format = "json" | "csv";
2
+ export type V4Direction = "ASC" | "DESC";
3
+ export type V4Meta = {
4
+ request_id: string;
5
+ status_code: number;
6
+ success: boolean;
7
+ [key: string]: unknown;
8
+ };
9
+ export type V4Response<T> = {
10
+ meta: V4Meta;
11
+ data: T;
12
+ };
13
+ export type V4ErrorBody = {
14
+ meta?: Partial<V4Meta>;
15
+ error?: {
16
+ code: number;
17
+ message: string;
18
+ retryable?: boolean;
19
+ };
20
+ };
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ // Shared types for the Semrush API v4 (https://api.semrush.com/apis/v4/).
3
+ // v4 differs from v3: key goes in the `Authorization: Apikey <key>` header,
4
+ // responses are JSON envelopes ({ meta, data }) instead of CSV.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "semrush-api-client",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/index.js",
@@ -18,7 +18,9 @@
18
18
  "release": "bash scripts/release.sh",
19
19
  "release:minor": "bash scripts/release.sh minor",
20
20
  "release:major": "bash scripts/release.sh major",
21
- "test": "echo \"Error: no test specified\" && exit 1"
21
+ "test": "echo \"Error: no test specified\" && exit 1",
22
+ "test:endpoints": "tsx test/test_endpoints.ts",
23
+ "test:endpoints:v4": "tsx test/test_v4_endpoints.ts"
22
24
  },
23
25
  "engines": {
24
26
  "node": ">=20"
@@ -38,6 +40,7 @@
38
40
  ],
39
41
  "devDependencies": {
40
42
  "@types/node": "^22.15.30",
43
+ "tsx": "^4.23.1",
41
44
  "typescript": "^5.9.3"
42
45
  }
43
46
  }