> For the complete documentation index, see [llms.txt](https://docs.gameket.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gameket.io/operations/products/common-operations.md).

# Common Operations

#### Create and Set Stock

{% tabs %}
{% tab title="Javascript" %}
{% code expandable="true" %}

```javascript
// 1. Create product
const createResponse = await fetch('https://api.gameket.io/merchant/products', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${token}`,
    'x-merchant-api-key': apiKey,
    'Idempotency-Key': uuid(),
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    name: 'FIFA 2026 Coins',
    price: 50,
    type: 'game-vouchers'
  })
});

const { data: product } = await createResponse.json();

// 2. Set initial stock
await fetch(`https://api.gameket.io/merchant/products/stock?productId=${product.productId}`, {
  method: 'PUT',
  headers: {
    'Authorization': `Bearer ${token}`,
    'x-merchant-api-key': apiKey,
    'Idempotency-Key': uuid(),
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    available: 1000,
    method: 'manual'
  })
});
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code expandable="true" %}

```python
import requests
from uuid import uuid4

def create_product_and_set_stock(token, api_key):
    # 1. Create product
    create_response = requests.post(
        'https://api.gameket.io/merchant/products',
        headers={
            'Authorization': f'Bearer {token}',
            'x-merchant-api-key': api_key,
            'Idempotency-Key': str(uuid4()),
            'Content-Type': 'application/json'
        },
        json={
            'name': 'FIFA 2026 Coins',
            'price': 50,
            'type': 'game-vouchers'
        }
    )
    
    product = create_response.json()['data']
    product_id = product['productId']
    
    # 2. Set initial stock
    requests.put(
        f'https://api.gameket.io/merchant/products/stock?productId={product_id}',
        headers={
            'Authorization': f'Bearer {token}',
            'x-merchant-api-key': api_key,
            'Idempotency-Key': str(uuid4()),
            'Content-Type': 'application/json'
        },
        json={
            'available': 1000,
            'method': 'manual'
        }
    )
    
    return product_id
```

{% endcode %}
{% endtab %}
{% endtabs %}

#### Enable Auto Stock Management

{% tabs %}
{% tab title="Javascript" %}
{% code expandable="true" %}

```javascript
await fetch(`https://api.gameket.io/merchant/products/stock/auto?productId=${productId}`, {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${token}`,
    'x-merchant-api-key': apiKey,
    'Idempotency-Key': uuid(),
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    enabled: true,
    minThreshold: 100,
    autoReplenishAmount: 500
  })
});
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code expandable="true" %}

```python

def enable_auto_stock(token, api_key, product_id):
    """Enable automatic stock management"""
    response = requests.post(
        f'https://api.gameket.io/merchant/products/stock/auto?productId={product_id}',
        headers={
            'Authorization': f'Bearer {token}',
            'x-merchant-api-key': api_key,
            'Idempotency-Key': str(uuid4()),
            'Content-Type': 'application/json'
        },
        json={
            'enabled': True,
            'minThreshold': 100,
            'autoReplenishAmount': 500
        }
    )
    return response.json()
```

{% endcode %}
{% endtab %}
{% endtabs %}

#### Deactivate Product

{% tabs %}
{% tab title="Javascript" %}
{% code expandable="true" %}

```javascript
await fetch(`https://api.gameket.io/merchant/products/status?productId=${productId}`, {
  method: 'PATCH',
  headers: {
    'Authorization': `Bearer ${token}`,
    'x-merchant-api-key': apiKey,
    'Idempotency-Key': uuid(),
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    status: 'inactive'
  })
});
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code expandable="true" %}

```python
def deactivate_product(token, api_key, product_id):
    """Deactivate a product"""
    response = requests.patch(
        f'https://api.gameket.io/merchant/products/status?productId={product_id}',
        headers={
            'Authorization': f'Bearer {token}',
            'x-merchant-api-key': api_key,
            'Idempotency-Key': str(uuid4()),
            'Content-Type': 'application/json'
        },
        json={'status': 'inactive'}
    )
    return response.json()
```

{% endcode %}
{% endtab %}
{% endtabs %}

#### Rust: Create and Set Stock

{% code expandable="true" %}

```rust
use reqwest::Client;
use uuid::Uuid;
use serde_json::json;

pub struct ProductManager {
    client: Client,
    api_key: String,
    token: String,
}

impl ProductManager {
    pub fn new(client: Client, api_key: String, token: String) -> Self {
        Self {
            client,
            api_key,
            token,
        }
    }

    async fn create_product_and_set_stock(
        &self,
        name: &str,
        price: f64,
        product_type: &str,
    ) -> Result<String, Box<dyn std::error::Error>> {
        // 1. Create product
        let create_response = self
            .client
            .post("https://api.gameket.io/merchant/products")
            .header("Authorization", format!("Bearer {}", self.token))
            .header("x-merchant-api-key", &self.api_key)
            .header("Idempotency-Key", Uuid::new_v4().to_string())
            .json(&json!({
                "name": name,
                "price": price,
                "type": product_type
            }))
            .send()
            .await?;

        let product_data: serde_json::Value = create_response.json().await?;
        let product_id = product_data["data"]["productId"]
            .as_str()
            .unwrap()
            .to_string();

        // 2. Set initial stock
        self.client
            .put(format!(
                "https://api.gameket.io/merchant/products/stock?productId={}",
                product_id
            ))
            .header("Authorization", format!("Bearer {}", self.token))
            .header("x-merchant-api-key", &self.api_key)
            .header("Idempotency-Key", Uuid::new_v4().to_string())
            .json(&json!({
                "available": 1000,
                "method": "manual"
            }))
            .send()
            .await?;

        Ok(product_id)
    }

    async fn enable_auto_stock(
        &self,
        product_id: &str,
        min_threshold: u32,
        auto_replenish: u32,
    ) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
        let response = self
            .client
            .post(format!(
                "https://api.gameket.io/merchant/products/stock/auto?productId={}",
                product_id
            ))
            .header("Authorization", format!("Bearer {}", self.token))
            .header("x-merchant-api-key", &self.api_key)
            .header("Idempotency-Key", Uuid::new_v4().to_string())
            .json(&json!({
                "enabled": true,
                "minThreshold": min_threshold,
                "autoReplenishAmount": auto_replenish
            }))
            .send()
            .await?;

        Ok(response.json().await?)
    }

    async fn deactivate_product(
        &self,
        product_id: &str,
    ) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
        let response = self
            .client
            .patch(format!(
                "https://api.gameket.io/merchant/products/status?productId={}",
                product_id
            ))
            .header("Authorization", format!("Bearer {}", self.token))
            .header("x-merchant-api-key", &self.api_key)
            .header("Idempotency-Key", Uuid::new_v4().to_string())
            .json(&json!({ "status": "inactive" }))
            .send()
            .await?;

        Ok(response.json().await?)
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();
    let api_key = std::env::var("MERCHANT_API_KEY")?;
    let token = std::env::var("MERCHANT_TOKEN")?;

    let manager = ProductManager::new(client, api_key, token);

    // Create product
    let product_id =
        manager
            .create_product_and_set_stock("FIFA 2026 Coins", 50.0, "game-vouchers")
            .await?;

    println!("Created product: {}", product_id);

    // Enable auto stock
    manager
        .enable_auto_stock(&product_id, 100, 500)
        .await?;

    println!("Enabled auto-stock management");

    Ok(())
}
```

{% endcode %}

### Error Responses

#### 400 Bad Request

```json
{
  "success": false,
  "error": "Invalid product type",
  "code": "INVALID_TYPE",
  "requestId": "req_abc123"
}
```

#### 404 Not Found

```json
{
  "success": false,
  "error": "Product not found",
  "code": "PRODUCT_NOT_FOUND",
  "requestId": "req_def456"
}
```

#### 409 Conflict

```json
{
  "success": false,
  "error": "Product already exists",
  "code": "PRODUCT_EXISTS",
  "requestId": "req_ghi789"
}
```
