| Crates.io | google-indexing-api |
| lib.rs | google-indexing-api |
| version | 1.0.0 |
| created_at | 2023-05-23 23:31:44.830292+00 |
| updated_at | 2025-12-20 07:41:16.413175+00 |
| description | GoogleIndexing API |
| homepage | |
| repository | https://github.com/uiuifree/rust-google-indexing-api |
| max_upload_size | |
| id | 872459 |
| size | 74,923 |
A Rust library for interfacing with the Google Indexing API. Notify Google when pages are added, updated, or deleted on your website for faster indexing in search results.
Before using this library, you need to:
For detailed setup instructions, see the Google Indexing API documentation.
Add this to your Cargo.toml:
[dependencies]
google-indexing-api = "1.0"
tokio = { version = "1", features = ["full"] }
yup-oauth2 = "12" # For authentication
use google_indexing_api::{GoogleIndexingApi, UrlNotificationsType};
use yup_oauth2::ServiceAccountAuthenticator;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load service account credentials
let secret = yup_oauth2::read_service_account_key("service-account-key.json")
.await
.expect("Failed to read service account key");
// Create authenticator
let auth = ServiceAccountAuthenticator::builder(secret)
.build()
.await?;
// Get access token
let scopes = &["https://www.googleapis.com/auth/indexing"];
let token = auth.token(scopes).await?;
let token_str = token.token().unwrap();
// Initialize API client
let api = GoogleIndexingApi::url_notifications();
// Notify Google about a URL update
let response = api.publish(
token_str,
"https://example.com/page1",
UrlNotificationsType::UPDATED
).await?;
println!("Successfully notified Google about the URL update");
Ok(())
}
use google_indexing_api::{GoogleIndexingApi, UrlNotificationsType};
let api = GoogleIndexingApi::url_notifications();
// Notify about an updated URL
let response = api.publish(
token_str,
"https://example.com/new-article",
UrlNotificationsType::UPDATED
).await?;
// Notify about a deleted URL
let response = api.publish(
token_str,
"https://example.com/old-article",
UrlNotificationsType::DELETED
).await?;
// Get metadata about a URL
let metadata = api.get_metadata(
token_str,
"https://example.com/article"
).await?;
Process up to 100 URLs in a single request for better performance:
use google_indexing_api::{GoogleIndexingApi, UrlNotificationsType};
let api = GoogleIndexingApi::url_notifications();
let urls = vec![
"https://example.com/page1".to_string(),
"https://example.com/page2".to_string(),
"https://example.com/page3".to_string(),
];
let batch_response = api.batch(
token_str,
urls,
UrlNotificationsType::UPDATED
).await?;
// Process batch results
for result in batch_response {
println!("URL: {}", result.url());
println!("Status Code: {}", result.status_code);
if let Some(json) = result.json() {
println!("Response: {:?}", json);
}
}
GoogleIndexingApi::url_notifications()Creates a new API client for URL notifications.
publish(token: &str, url: &str, notification_type: UrlNotificationsType) -> Result<T, GoogleApiError>Notify Google about a single URL update or deletion.
Parameters:
token: OAuth2 access tokenurl: The URL to notify Google aboutnotification_type: Either UrlNotificationsType::UPDATED or UrlNotificationsType::DELETEDget_metadata(token: &str, url: &str) -> Result<T, GoogleApiError>Retrieve metadata about a URL from Google's index.
Parameters:
token: OAuth2 access tokenurl: The URL to get metadata forbatch(token: &str, urls: Vec<String>, notification_type: UrlNotificationsType) -> Result<Vec<ResponseGoogleIndexingBatch>, GoogleApiError>Notify Google about multiple URLs in a single batch request.
Parameters:
token: OAuth2 access tokenurls: Vector of URLs (up to 100)notification_type: Either UrlNotificationsType::UPDATED or UrlNotificationsType::DELETEDpub enum UrlNotificationsType {
UPDATED, // Notify that a URL has been updated or added
DELETED, // Notify that a URL has been deleted
}
The library provides comprehensive error handling through the GoogleApiError enum:
use google_indexing_api::GoogleApiError;
match api.publish(token_str, url, UrlNotificationsType::UPDATED).await {
Ok(response) => println!("Success: {:?}", response),
Err(GoogleApiError::Connection(e)) => eprintln!("Connection error: {}", e),
Err(GoogleApiError::JsonParse(e)) => eprintln!("JSON parse error: {}", e),
Err(e) => eprintln!("Other error: {:?}", e),
}
The Google Indexing API has the following quotas:
For batch operations, each URL in the batch counts toward your quota.
See the tests directory for more complete examples including:
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.