update_price/
update_price.rs

1use std::collections::HashMap;
2
3use amazon_spapi::client::{SpapiClient, SpapiConfig};
4use amazon_spapi::models::listings_items_2021_08_01::patch_operation::Op;
5use amazon_spapi::models::listings_items_2021_08_01::{ListingsItemPatchRequest, PatchOperation};
6use anyhow::Result;
7use serde_json::json;
8
9#[tokio::main]
10async fn main() -> Result<()> {
11    let config = SpapiConfig::from_env()?;
12    let client = SpapiClient::new(config.clone())?;
13
14    let seller_id = "YOUR_SELLER_ID";
15    let sku = "YOUR_SKU";
16    let marketplace_ids = vec!["ATVPDKIKX0DER".to_string()]; // Amazon US Marketplace ID
17    let new_price = 42.0; // New price to set
18
19    log::info!(
20        "Updating price for SKU: {} in marketplace: {:?}",
21        sku,
22        marketplace_ids
23    );
24    match update_listing_price(&client, seller_id, sku, &marketplace_ids, new_price).await {
25        Ok(response) => {
26            println!(
27                "Price update request submitted successfully! {:#?}",
28                response
29            );
30            println!("Submission ID: {}", response.submission_id);
31            if let Some(issues) = response.issues {
32                if !issues.is_empty() {
33                    println!("Issues found:");
34                    for issue in issues {
35                        println!("  - {}: {}", issue.code, issue.message);
36                    }
37                }
38            }
39        }
40        Err(e) => {
41            eprintln!("Failed to update price: {}", e);
42        }
43    }
44
45    let item = get_current_listing(&client, seller_id, sku, &marketplace_ids).await?;
46    println!("Current listing information: {:#?}", item);
47
48    Ok(())
49}
50
51/// Update the listing price
52async fn update_listing_price(
53    client: &SpapiClient,
54    seller_id: &str,
55    sku: &str,
56    marketplace_ids: &[String],
57    new_price: f64,
58) -> Result<amazon_spapi::models::listings_items_2021_08_01::ListingsItemSubmissionResponse> {
59    let mut purchasable_offer = HashMap::new();
60    purchasable_offer.insert("audience".to_string(), json!("ALL"));
61    purchasable_offer.insert("currency".to_string(), json!("USD"));
62    purchasable_offer.insert("marketplace_id".to_string(), json!(marketplace_ids[0]));
63    purchasable_offer.insert(
64        "our_price".to_string(),
65        json!([{
66            "schedule": [{
67                "value_with_tax": new_price
68            }]
69        }]),
70    );
71
72    let mut list_price = HashMap::new();
73    list_price.insert("marketplace_id".to_string(), json!(marketplace_ids[0]));
74    list_price.insert("currency".to_string(), json!("USD"));
75    list_price.insert("value".to_string(), json!(new_price));
76
77    let purchasable_offer_patch = PatchOperation {
78        op: Op::Replace,
79        path: "/attributes/purchasable_offer".to_string(),
80        value: Some(vec![purchasable_offer]),
81    };
82
83    let list_price_patch = PatchOperation {
84        op: Op::Replace,
85        path: "/attributes/list_price".to_string(),
86        value: Some(vec![list_price]),
87    };
88
89    let patch_request = ListingsItemPatchRequest {
90        product_type: "PRODUCT".to_string(),
91        patches: vec![purchasable_offer_patch, list_price_patch],
92    };
93
94    println!("{:#?}", patch_request);
95
96    client
97        .patch_listings_item(
98            seller_id,
99            sku,
100            marketplace_ids.to_vec(),
101            patch_request,
102            None, // included_data
103            None, // Some("VALIDATION_PREVIEW"), // mode - Can use "VALIDATION_PREVIEW" to validate first
104            None, // issue_locale
105        )
106        .await
107}
108
109/// Fetch the current listing information for a given SKU
110async fn get_current_listing(
111    client: &SpapiClient,
112    seller_id: &str,
113    sku: &str,
114    marketplace_ids: &[String],
115) -> Result<amazon_spapi::models::listings_items_2021_08_01::Item> {
116    client
117        .get_listings_item(
118            seller_id,
119            sku,
120            marketplace_ids.to_vec(),
121            None,                                                          // issue_locale
122            Some(vec!["summaries".to_string(), "attributes".to_string()]), // included_data
123        )
124        .await
125}