google-search-console-cli 1.0.0
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.
- package/LICENSE +201 -0
- package/README.md +239 -0
- package/dist/auth.js +49 -0
- package/dist/commands/inspect.js +22 -0
- package/dist/commands/query.js +71 -0
- package/dist/commands/sitemaps.js +52 -0
- package/dist/commands/sites.js +48 -0
- package/dist/index.js +53 -0
- package/dist/utils.js +26 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. Please also get an in-place
|
|
186
|
+
temporary or permanent copyright header for your project as
|
|
187
|
+
described on https://www.apache.org/legal/src-headers.html
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Benn Huang
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
# google-search-console-cli
|
|
2
|
+
|
|
3
|
+
A Google Search Console CLI designed for AI agents. Wraps the official Search Console API with simple, agent-friendly commands.
|
|
4
|
+
|
|
5
|
+
**Works with:** OpenClaw, Claude Code, Cursor, Codex, and any agent that can run shell commands.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install -g google-search-console-cli
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or run directly with npx:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npx google-search-console-cli --help
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
For development:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pnpm install
|
|
23
|
+
pnpm build
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## How it works
|
|
27
|
+
|
|
28
|
+
This CLI is a thin wrapper around Google's official APIs:
|
|
29
|
+
|
|
30
|
+
- **[Search Console API v1](https://developers.google.com/webmaster-tools/v1/api_reference_index)** — URL inspection (`inspect`)
|
|
31
|
+
- **[Webmasters API v3](https://developers.google.com/webmaster-tools/v1/api_reference_index)** — search analytics, sites, and sitemaps (`query`, `sites`, `sitemaps`)
|
|
32
|
+
|
|
33
|
+
Under the hood it uses the official Node.js client library [`googleapis`](https://www.npmjs.com/package/googleapis). All API responses are passed through as JSON.
|
|
34
|
+
|
|
35
|
+
## Setup
|
|
36
|
+
|
|
37
|
+
### Step 1: Enable the Search Console API
|
|
38
|
+
|
|
39
|
+
Go to the [Google Cloud Console API Library](https://console.cloud.google.com/apis/library/searchconsole.googleapis.com) and enable the **Google Search Console API** for your project. If you don't have a project yet, create one first.
|
|
40
|
+
|
|
41
|
+
### Step 2: Create a Service Account
|
|
42
|
+
|
|
43
|
+
1. Go to [IAM & Admin > Service Accounts](https://console.cloud.google.com/iam-admin/serviceaccounts) in the same project.
|
|
44
|
+
2. Click **Create Service Account**, give it a name (e.g. `search-console-reader`), and click **Done**.
|
|
45
|
+
3. Click on the newly created Service Account, go to the **Keys** tab.
|
|
46
|
+
4. Click **Add Key > Create new key > JSON**, and download the key file.
|
|
47
|
+
|
|
48
|
+
### Step 3: Place the credentials file
|
|
49
|
+
|
|
50
|
+
Choose one of these options:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
# Option A: Default path (recommended)
|
|
54
|
+
mkdir -p ~/.config/google-search-console-cli
|
|
55
|
+
cp ~/Downloads/your-key-file.json ~/.config/google-search-console-cli/credentials.json
|
|
56
|
+
|
|
57
|
+
# Option B: Environment variable
|
|
58
|
+
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your-key-file.json"
|
|
59
|
+
|
|
60
|
+
# Option C: Pass per command
|
|
61
|
+
google-search-console-cli sites --credentials /path/to/your-key-file.json
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Credentials are resolved in this order:
|
|
65
|
+
1. `--credentials <path>` flag
|
|
66
|
+
2. `GOOGLE_APPLICATION_CREDENTIALS` env var
|
|
67
|
+
3. `~/.config/google-search-console-cli/credentials.json` (auto-detected)
|
|
68
|
+
4. gcloud Application Default Credentials
|
|
69
|
+
|
|
70
|
+
### Step 4: Grant access in Search Console
|
|
71
|
+
|
|
72
|
+
The Service Account needs permission to access your sites in Search Console. This must be done **per site**.
|
|
73
|
+
|
|
74
|
+
1. Open [Google Search Console](https://search.google.com/search-console).
|
|
75
|
+
2. Select a site you want to grant access to.
|
|
76
|
+
3. Go to **Settings** (bottom-left) > **Users and permissions**.
|
|
77
|
+
4. Click **Add user**.
|
|
78
|
+
5. Enter the Service Account email (find it in your key file's `client_email` field, e.g. `my-sa@my-project.iam.gserviceaccount.com`).
|
|
79
|
+
6. Choose a permission level:
|
|
80
|
+
- **Restricted** (read-only): can use `sites`, `site`, `query`, `sitemaps`, `sitemap`, `inspect`
|
|
81
|
+
- **Full**: can also use `site-add`, `site-remove`, `sitemap-submit`, `sitemap-delete`
|
|
82
|
+
7. Click **Add**.
|
|
83
|
+
|
|
84
|
+
Repeat for each site you want to access. There is no global "add to all sites" option in Search Console.
|
|
85
|
+
|
|
86
|
+
### Alternative: gcloud ADC (for local development)
|
|
87
|
+
|
|
88
|
+
If you prefer not to use a Service Account, you can authenticate with your own Google account:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
gcloud auth application-default login \
|
|
92
|
+
--scopes="https://www.googleapis.com/auth/webmasters.readonly"
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
This uses your personal Google account's Search Console access. Good for local development, not recommended for automation.
|
|
96
|
+
|
|
97
|
+
## Usage
|
|
98
|
+
|
|
99
|
+
All commands output pretty-printed JSON by default. Use `--format compact` for compact single-line JSON.
|
|
100
|
+
|
|
101
|
+
Site URLs follow the Search Console format:
|
|
102
|
+
- **URL-prefix property**: `https://www.example.com/` (must include trailing slash)
|
|
103
|
+
- **Domain property**: `sc-domain:example.com`
|
|
104
|
+
|
|
105
|
+
### query
|
|
106
|
+
|
|
107
|
+
Query search analytics data with filters and dimensions.
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
# Basic query: top queries for the last 30 days
|
|
111
|
+
google-search-console-cli query https://www.example.com/ \
|
|
112
|
+
--start-date 2025-01-01 \
|
|
113
|
+
--end-date 2025-01-31 \
|
|
114
|
+
--dimensions query
|
|
115
|
+
|
|
116
|
+
# Top pages by country
|
|
117
|
+
google-search-console-cli query sc-domain:example.com \
|
|
118
|
+
--start-date 2025-01-01 \
|
|
119
|
+
--end-date 2025-01-31 \
|
|
120
|
+
--dimensions page,country \
|
|
121
|
+
--row-limit 100
|
|
122
|
+
|
|
123
|
+
# With dimension filters
|
|
124
|
+
google-search-console-cli query https://www.example.com/ \
|
|
125
|
+
--start-date 2025-01-01 \
|
|
126
|
+
--end-date 2025-01-31 \
|
|
127
|
+
--dimensions query \
|
|
128
|
+
--dimension-filter '[{"groupType":"and","filters":[{"dimension":"country","operator":"equals","expression":"USA"}]}]'
|
|
129
|
+
|
|
130
|
+
# Image search results
|
|
131
|
+
google-search-console-cli query https://www.example.com/ \
|
|
132
|
+
--start-date 2025-01-01 \
|
|
133
|
+
--end-date 2025-01-31 \
|
|
134
|
+
--dimensions query \
|
|
135
|
+
--type image
|
|
136
|
+
|
|
137
|
+
# Include fresh (unfinalized) data
|
|
138
|
+
google-search-console-cli query https://www.example.com/ \
|
|
139
|
+
--start-date 2025-01-01 \
|
|
140
|
+
--end-date 2025-01-31 \
|
|
141
|
+
--dimensions date \
|
|
142
|
+
--data-state all
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Available dimensions: `date`, `query`, `page`, `country`, `device`, `searchAppearance`
|
|
146
|
+
|
|
147
|
+
Available types: `web` (default), `image`, `video`, `news`, `discover`, `googleNews`
|
|
148
|
+
|
|
149
|
+
### sites
|
|
150
|
+
|
|
151
|
+
List all sites in your Search Console account.
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
google-search-console-cli sites
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### site
|
|
158
|
+
|
|
159
|
+
Get information about a specific site.
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
google-search-console-cli site https://www.example.com/
|
|
163
|
+
google-search-console-cli site sc-domain:example.com
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### site-add
|
|
167
|
+
|
|
168
|
+
Add a site to Search Console. Requires **Full** permission.
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
google-search-console-cli site-add https://www.example.com/
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### site-remove
|
|
175
|
+
|
|
176
|
+
Remove a site from Search Console. Requires **Full** permission.
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
google-search-console-cli site-remove https://www.example.com/
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### sitemaps
|
|
183
|
+
|
|
184
|
+
List sitemaps for a site.
|
|
185
|
+
|
|
186
|
+
```bash
|
|
187
|
+
google-search-console-cli sitemaps https://www.example.com/
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### sitemap
|
|
191
|
+
|
|
192
|
+
Get information about a specific sitemap.
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
google-search-console-cli sitemap https://www.example.com/ https://www.example.com/sitemap.xml
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### sitemap-submit
|
|
199
|
+
|
|
200
|
+
Submit a sitemap for a site. Requires **Full** permission.
|
|
201
|
+
|
|
202
|
+
```bash
|
|
203
|
+
google-search-console-cli sitemap-submit https://www.example.com/ https://www.example.com/sitemap.xml
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### sitemap-delete
|
|
207
|
+
|
|
208
|
+
Delete a sitemap from a site. Requires **Full** permission.
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
google-search-console-cli sitemap-delete https://www.example.com/ https://www.example.com/sitemap.xml
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### inspect
|
|
215
|
+
|
|
216
|
+
Inspect a URL's index status in Google.
|
|
217
|
+
|
|
218
|
+
```bash
|
|
219
|
+
google-search-console-cli inspect https://www.example.com/ https://www.example.com/my-page
|
|
220
|
+
|
|
221
|
+
# With a specific language for messages
|
|
222
|
+
google-search-console-cli inspect https://www.example.com/ https://www.example.com/my-page --language zh-CN
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Error output
|
|
226
|
+
|
|
227
|
+
Errors are written to stderr as JSON with an `error` field. For Google API errors, `code` and `details` are included when available:
|
|
228
|
+
|
|
229
|
+
```json
|
|
230
|
+
{"error": "Permission denied", "code": 403}
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Related
|
|
234
|
+
|
|
235
|
+
- [google-analytics-cli](https://github.com/Bin-Huang/google-analytics-cli) — Google Analytics CLI for AI agents
|
|
236
|
+
|
|
237
|
+
## License
|
|
238
|
+
|
|
239
|
+
Apache-2.0
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { google } from "googleapis";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
const { version } = require("../package.json");
|
|
8
|
+
export { version };
|
|
9
|
+
const DEFAULT_CREDENTIALS_PATH = path.join(os.homedir(), ".config", "google-search-console-cli", "credentials.json");
|
|
10
|
+
let credentialsPath;
|
|
11
|
+
export function setCredentialsPath(p) {
|
|
12
|
+
credentialsPath = p;
|
|
13
|
+
}
|
|
14
|
+
async function getAuthClient() {
|
|
15
|
+
const keyFile = credentialsPath
|
|
16
|
+
?? (!process.env.GOOGLE_APPLICATION_CREDENTIALS &&
|
|
17
|
+
fs.existsSync(DEFAULT_CREDENTIALS_PATH)
|
|
18
|
+
? DEFAULT_CREDENTIALS_PATH
|
|
19
|
+
: undefined);
|
|
20
|
+
const auth = new google.auth.GoogleAuth({
|
|
21
|
+
keyFile,
|
|
22
|
+
scopes: ["https://www.googleapis.com/auth/webmasters.readonly"],
|
|
23
|
+
});
|
|
24
|
+
return auth;
|
|
25
|
+
}
|
|
26
|
+
async function getWriteAuthClient() {
|
|
27
|
+
const keyFile = credentialsPath
|
|
28
|
+
?? (!process.env.GOOGLE_APPLICATION_CREDENTIALS &&
|
|
29
|
+
fs.existsSync(DEFAULT_CREDENTIALS_PATH)
|
|
30
|
+
? DEFAULT_CREDENTIALS_PATH
|
|
31
|
+
: undefined);
|
|
32
|
+
const auth = new google.auth.GoogleAuth({
|
|
33
|
+
keyFile,
|
|
34
|
+
scopes: ["https://www.googleapis.com/auth/webmasters"],
|
|
35
|
+
});
|
|
36
|
+
return auth;
|
|
37
|
+
}
|
|
38
|
+
export async function createSearchConsoleClient() {
|
|
39
|
+
const auth = await getAuthClient();
|
|
40
|
+
return google.searchconsole({ version: "v1", auth });
|
|
41
|
+
}
|
|
42
|
+
export async function createWebmastersClient() {
|
|
43
|
+
const auth = await getAuthClient();
|
|
44
|
+
return google.webmasters({ version: "v3", auth });
|
|
45
|
+
}
|
|
46
|
+
export async function createWriteWebmastersClient() {
|
|
47
|
+
const auth = await getWriteAuthClient();
|
|
48
|
+
return google.webmasters({ version: "v3", auth });
|
|
49
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createSearchConsoleClient } from "../auth.js";
|
|
2
|
+
import { run } from "../utils.js";
|
|
3
|
+
export function registerInspectCommands(program) {
|
|
4
|
+
program
|
|
5
|
+
.command("inspect <siteUrl> <inspectionUrl>")
|
|
6
|
+
.description("Inspect a URL's index status")
|
|
7
|
+
.option("--language <code>", "Language code for messages (e.g. en-US)", "en-US")
|
|
8
|
+
.action(async (siteUrl, inspectionUrl, opts, cmd) => {
|
|
9
|
+
const format = cmd.optsWithGlobals().format;
|
|
10
|
+
await run(async () => {
|
|
11
|
+
const client = await createSearchConsoleClient();
|
|
12
|
+
const res = await client.urlInspection.index.inspect({
|
|
13
|
+
requestBody: {
|
|
14
|
+
inspectionUrl,
|
|
15
|
+
siteUrl,
|
|
16
|
+
languageCode: opts.language,
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
return res.data;
|
|
20
|
+
}, format);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { createWebmastersClient } from "../auth.js";
|
|
2
|
+
import { errorJson, run } from "../utils.js";
|
|
3
|
+
function parseJson(value) {
|
|
4
|
+
try {
|
|
5
|
+
return JSON.parse(value);
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
8
|
+
throw new Error(`Invalid JSON: ${value}`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function parsePositiveInt(value) {
|
|
12
|
+
const n = parseInt(value, 10);
|
|
13
|
+
if (Number.isNaN(n) || n < 0) {
|
|
14
|
+
throw new Error(`Expected a non-negative integer, got: ${value}`);
|
|
15
|
+
}
|
|
16
|
+
return n;
|
|
17
|
+
}
|
|
18
|
+
export function registerQueryCommands(program) {
|
|
19
|
+
program
|
|
20
|
+
.command("query <siteUrl>")
|
|
21
|
+
.description("Query search analytics data")
|
|
22
|
+
.requiredOption("--start-date <date>", "Start date (YYYY-MM-DD)")
|
|
23
|
+
.requiredOption("--end-date <date>", "End date (YYYY-MM-DD)")
|
|
24
|
+
.option("--dimensions <names>", "Comma-separated dimensions (date,query,page,country,device,searchAppearance)")
|
|
25
|
+
.option("--type <type>", "Search type (web,image,video,news,discover,googleNews)", "web")
|
|
26
|
+
.option("--dimension-filter <json>", "JSON array of dimension filter groups")
|
|
27
|
+
.option("--aggregation-type <type>", "Aggregation type (auto,byPage,byProperty)")
|
|
28
|
+
.option("--row-limit <n>", "Max rows (1-25000, default 1000)", parsePositiveInt)
|
|
29
|
+
.option("--start-row <n>", "Starting row offset (default 0)", parsePositiveInt)
|
|
30
|
+
.option("--data-state <state>", "Data freshness (all,final,hourly_all)")
|
|
31
|
+
.action(async (siteUrl, opts, cmd) => {
|
|
32
|
+
const format = cmd.optsWithGlobals().format;
|
|
33
|
+
await run(async () => {
|
|
34
|
+
const requestBody = {
|
|
35
|
+
startDate: opts.startDate,
|
|
36
|
+
endDate: opts.endDate,
|
|
37
|
+
};
|
|
38
|
+
if (opts.dimensions) {
|
|
39
|
+
requestBody.dimensions = opts.dimensions.split(",").map((s) => s.trim());
|
|
40
|
+
}
|
|
41
|
+
if (opts.type && opts.type !== "web") {
|
|
42
|
+
requestBody.type = opts.type;
|
|
43
|
+
}
|
|
44
|
+
if (opts.dimensionFilter) {
|
|
45
|
+
const filters = parseJson(opts.dimensionFilter);
|
|
46
|
+
if (!Array.isArray(filters)) {
|
|
47
|
+
errorJson("--dimension-filter must be a JSON array of filter groups");
|
|
48
|
+
}
|
|
49
|
+
requestBody.dimensionFilterGroups = filters;
|
|
50
|
+
}
|
|
51
|
+
if (opts.aggregationType) {
|
|
52
|
+
requestBody.aggregationType = opts.aggregationType;
|
|
53
|
+
}
|
|
54
|
+
if (opts.rowLimit != null) {
|
|
55
|
+
requestBody.rowLimit = opts.rowLimit;
|
|
56
|
+
}
|
|
57
|
+
if (opts.startRow != null) {
|
|
58
|
+
requestBody.startRow = opts.startRow;
|
|
59
|
+
}
|
|
60
|
+
if (opts.dataState) {
|
|
61
|
+
requestBody.dataState = opts.dataState;
|
|
62
|
+
}
|
|
63
|
+
const client = await createWebmastersClient();
|
|
64
|
+
const res = await client.searchanalytics.query({
|
|
65
|
+
siteUrl,
|
|
66
|
+
requestBody,
|
|
67
|
+
});
|
|
68
|
+
return res.data;
|
|
69
|
+
}, format);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { createWebmastersClient, createWriteWebmastersClient } from "../auth.js";
|
|
2
|
+
import { run } from "../utils.js";
|
|
3
|
+
export function registerSitemapsCommands(program) {
|
|
4
|
+
program
|
|
5
|
+
.command("sitemaps <siteUrl>")
|
|
6
|
+
.description("List sitemaps for a site")
|
|
7
|
+
.option("--sitemap-index <url>", "Only list sitemaps under this sitemap index")
|
|
8
|
+
.action(async (siteUrl, opts, cmd) => {
|
|
9
|
+
const format = cmd.optsWithGlobals().format;
|
|
10
|
+
await run(async () => {
|
|
11
|
+
const client = await createWebmastersClient();
|
|
12
|
+
const params = { siteUrl };
|
|
13
|
+
if (opts.sitemapIndex)
|
|
14
|
+
params.sitemapIndex = opts.sitemapIndex;
|
|
15
|
+
const res = await client.sitemaps.list(params);
|
|
16
|
+
return res.data.sitemap ?? [];
|
|
17
|
+
}, format);
|
|
18
|
+
});
|
|
19
|
+
program
|
|
20
|
+
.command("sitemap <siteUrl> <feedpath>")
|
|
21
|
+
.description("Get information about a specific sitemap")
|
|
22
|
+
.action(async (siteUrl, feedpath, _opts, cmd) => {
|
|
23
|
+
const format = cmd.optsWithGlobals().format;
|
|
24
|
+
await run(async () => {
|
|
25
|
+
const client = await createWebmastersClient();
|
|
26
|
+
const res = await client.sitemaps.get({ siteUrl, feedpath });
|
|
27
|
+
return res.data;
|
|
28
|
+
}, format);
|
|
29
|
+
});
|
|
30
|
+
program
|
|
31
|
+
.command("sitemap-submit <siteUrl> <feedpath>")
|
|
32
|
+
.description("Submit a sitemap for a site")
|
|
33
|
+
.action(async (siteUrl, feedpath, _opts, cmd) => {
|
|
34
|
+
const format = cmd.optsWithGlobals().format;
|
|
35
|
+
await run(async () => {
|
|
36
|
+
const client = await createWriteWebmastersClient();
|
|
37
|
+
await client.sitemaps.submit({ siteUrl, feedpath });
|
|
38
|
+
return { submitted: feedpath, site: siteUrl };
|
|
39
|
+
}, format);
|
|
40
|
+
});
|
|
41
|
+
program
|
|
42
|
+
.command("sitemap-delete <siteUrl> <feedpath>")
|
|
43
|
+
.description("Delete a sitemap from a site")
|
|
44
|
+
.action(async (siteUrl, feedpath, _opts, cmd) => {
|
|
45
|
+
const format = cmd.optsWithGlobals().format;
|
|
46
|
+
await run(async () => {
|
|
47
|
+
const client = await createWriteWebmastersClient();
|
|
48
|
+
await client.sitemaps.delete({ siteUrl, feedpath });
|
|
49
|
+
return { deleted: feedpath, site: siteUrl };
|
|
50
|
+
}, format);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createWebmastersClient, createWriteWebmastersClient } from "../auth.js";
|
|
2
|
+
import { run } from "../utils.js";
|
|
3
|
+
export function registerSitesCommands(program) {
|
|
4
|
+
program
|
|
5
|
+
.command("sites")
|
|
6
|
+
.description("List all sites in Search Console")
|
|
7
|
+
.action(async (_opts, cmd) => {
|
|
8
|
+
const format = cmd.optsWithGlobals().format;
|
|
9
|
+
await run(async () => {
|
|
10
|
+
const client = await createWebmastersClient();
|
|
11
|
+
const res = await client.sites.list();
|
|
12
|
+
return res.data.siteEntry ?? [];
|
|
13
|
+
}, format);
|
|
14
|
+
});
|
|
15
|
+
program
|
|
16
|
+
.command("site <siteUrl>")
|
|
17
|
+
.description("Get information about a specific site")
|
|
18
|
+
.action(async (siteUrl, _opts, cmd) => {
|
|
19
|
+
const format = cmd.optsWithGlobals().format;
|
|
20
|
+
await run(async () => {
|
|
21
|
+
const client = await createWebmastersClient();
|
|
22
|
+
const res = await client.sites.get({ siteUrl });
|
|
23
|
+
return res.data;
|
|
24
|
+
}, format);
|
|
25
|
+
});
|
|
26
|
+
program
|
|
27
|
+
.command("site-add <siteUrl>")
|
|
28
|
+
.description("Add a site to Search Console")
|
|
29
|
+
.action(async (siteUrl, _opts, cmd) => {
|
|
30
|
+
const format = cmd.optsWithGlobals().format;
|
|
31
|
+
await run(async () => {
|
|
32
|
+
const client = await createWriteWebmastersClient();
|
|
33
|
+
await client.sites.add({ siteUrl });
|
|
34
|
+
return { added: siteUrl };
|
|
35
|
+
}, format);
|
|
36
|
+
});
|
|
37
|
+
program
|
|
38
|
+
.command("site-remove <siteUrl>")
|
|
39
|
+
.description("Remove a site from Search Console")
|
|
40
|
+
.action(async (siteUrl, _opts, cmd) => {
|
|
41
|
+
const format = cmd.optsWithGlobals().format;
|
|
42
|
+
await run(async () => {
|
|
43
|
+
const client = await createWriteWebmastersClient();
|
|
44
|
+
await client.sites.delete({ siteUrl });
|
|
45
|
+
return { removed: siteUrl };
|
|
46
|
+
}, format);
|
|
47
|
+
});
|
|
48
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command, CommanderError } from "commander";
|
|
3
|
+
import { setCredentialsPath, version } from "./auth.js";
|
|
4
|
+
import { registerSitesCommands } from "./commands/sites.js";
|
|
5
|
+
import { registerSitemapsCommands } from "./commands/sitemaps.js";
|
|
6
|
+
import { registerQueryCommands } from "./commands/query.js";
|
|
7
|
+
import { registerInspectCommands } from "./commands/inspect.js";
|
|
8
|
+
async function main() {
|
|
9
|
+
const program = new Command();
|
|
10
|
+
program
|
|
11
|
+
.name("google-search-console-cli")
|
|
12
|
+
.description("Google Search Console CLI for AI agents")
|
|
13
|
+
.addHelpText("after", "\nDocs: https://github.com/Bin-Huang/google-search-console-cli")
|
|
14
|
+
.version(version)
|
|
15
|
+
.option("--format <format>", "Output format", (value) => {
|
|
16
|
+
if (!["json", "compact"].includes(value)) {
|
|
17
|
+
throw new Error("Format must be 'json' or 'compact'.");
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
}, "json")
|
|
21
|
+
.option("--credentials <path>", "Path to service account JSON key file");
|
|
22
|
+
program.exitOverride();
|
|
23
|
+
program.configureOutput({
|
|
24
|
+
writeErr: (str) => process.stderr.write(JSON.stringify({ error: str.trim() }) + "\n"),
|
|
25
|
+
writeOut: (str) => process.stdout.write(str),
|
|
26
|
+
});
|
|
27
|
+
program.hook("preAction", (thisCommand) => {
|
|
28
|
+
const { credentials } = thisCommand.optsWithGlobals();
|
|
29
|
+
if (credentials)
|
|
30
|
+
setCredentialsPath(credentials);
|
|
31
|
+
});
|
|
32
|
+
registerQueryCommands(program);
|
|
33
|
+
registerSitesCommands(program);
|
|
34
|
+
registerSitemapsCommands(program);
|
|
35
|
+
registerInspectCommands(program);
|
|
36
|
+
// No args: show help and exit cleanly
|
|
37
|
+
if (process.argv.length <= 2) {
|
|
38
|
+
program.outputHelp();
|
|
39
|
+
process.exit(0);
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
await program.parseAsync();
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
if (err instanceof CommanderError) {
|
|
46
|
+
process.exit(err.exitCode);
|
|
47
|
+
}
|
|
48
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
49
|
+
process.stderr.write(JSON.stringify({ error: message }) + "\n");
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
main();
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export function outputJson(data, format) {
|
|
2
|
+
const indent = format === "json" ? 2 : undefined;
|
|
3
|
+
process.stdout.write(JSON.stringify(data ?? null, null, indent) + "\n");
|
|
4
|
+
}
|
|
5
|
+
export function errorJson(message, err) {
|
|
6
|
+
const output = { error: message };
|
|
7
|
+
if (err && typeof err === "object") {
|
|
8
|
+
const apiErr = err;
|
|
9
|
+
if (apiErr.code != null)
|
|
10
|
+
output.code = apiErr.code;
|
|
11
|
+
if (apiErr.details)
|
|
12
|
+
output.details = apiErr.details;
|
|
13
|
+
}
|
|
14
|
+
process.stderr.write(JSON.stringify(output) + "\n");
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|
|
17
|
+
export async function run(fn, format) {
|
|
18
|
+
try {
|
|
19
|
+
const data = await fn();
|
|
20
|
+
outputJson(data, format);
|
|
21
|
+
}
|
|
22
|
+
catch (err) {
|
|
23
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
24
|
+
errorJson(message, err);
|
|
25
|
+
}
|
|
26
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "google-search-console-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Google Search Console CLI for AI agents",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"google-search-console-cli": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc",
|
|
16
|
+
"dev": "tsx src/index.ts",
|
|
17
|
+
"prepack": "pnpm build"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/Bin-Huang/google-search-console-cli.git"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/Bin-Huang/google-search-console-cli/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/Bin-Huang/google-search-console-cli#readme",
|
|
27
|
+
"keywords": [
|
|
28
|
+
"google-search-console",
|
|
29
|
+
"gsc",
|
|
30
|
+
"search-console",
|
|
31
|
+
"seo",
|
|
32
|
+
"cli",
|
|
33
|
+
"ai-agent",
|
|
34
|
+
"google-webmasters-api"
|
|
35
|
+
],
|
|
36
|
+
"author": "Benn Huang <tohuangbin@gmail.com>",
|
|
37
|
+
"license": "Apache-2.0",
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=18"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"commander": "^14.0.3",
|
|
43
|
+
"googleapis": "^148.0.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^25.5.0",
|
|
47
|
+
"tsx": "^4.21.0",
|
|
48
|
+
"typescript": "^5.9.3"
|
|
49
|
+
},
|
|
50
|
+
"packageManager": "pnpm@10.32.1+sha512.a706938f0e89ac1456b6563eab4edf1d1faf3368d1191fc5c59790e96dc918e4456ab2e67d613de1043d2e8c81f87303e6b40d4ffeca9df15ef1ad567348f2be"
|
|
51
|
+
}
|