DEV Community

Cover image for วิธีใช้ Magento 2 API: คู่มือบูรณาการอีคอมเมิร์ซฉบับสมบูรณ์ (ปี 2026)
Thanawat Wongchai
Thanawat Wongchai

Posted on • Originally published at apidog.com

วิธีใช้ Magento 2 API: คู่มือบูรณาการอีคอมเมิร์ซฉบับสมบูรณ์ (ปี 2026)

สรุปโดยย่อ

API ของ Magento 2 (Adobe Commerce) ช่วยให้นักพัฒนาสามารถผสานรวมกับร้านค้าอีคอมเมิร์ซได้โดยทางโปรแกรม โดยใช้ REST, SOAP และ GraphQL endpoint พร้อมกับการยืนยันตัวตนแบบ OAuth 1.0a และ token-based authentication ซึ่งให้การเข้าถึงผลิตภัณฑ์, คำสั่งซื้อ, ลูกค้า, สินค้าคงคลัง และอื่นๆ อีกมากมาย พร้อมกับขีดจำกัดอัตราที่กำหนดค่าได้ คู่มือนี้ครอบคลุมการตั้งค่าการยืนยันตัวตน, การดำเนินการ CRUD, webhooks, custom endpoints และกลยุทธ์การผสานรวมสำหรับการใช้งานจริง

ทดลองใช้ Apidog วันนี้

บทนำ

Adobe Commerce (Magento) เป็นแพลตฟอร์มสำหรับร้านค้าอีคอมเมิร์ซมากกว่า 250,000 แห่ง ด้วยมูลค่าสินค้ารวมต่อปีมากกว่า 1.55 แสนล้านดอลลาร์ สำหรับนักพัฒนาที่สร้างการผสานรวมอีคอมเมิร์ซ, ตัวเชื่อมต่อ ERP หรือแอปพลิเคชันมือถือ การผสานรวม Magento API ไม่ใช่ทางเลือก แต่เป็นสิ่งจำเป็นสำหรับการเข้าถึงฐานผู้ค้าขนาดใหญ่นี้

ความเป็นจริงคือ: ผู้ค้าที่จัดการช่องทางการขายหลายช่องทางเสียเวลา 20-30 ชั่วโมงต่อสัปดาห์ในการป้อนข้อมูลด้วยตนเองระหว่าง Magento และระบบอื่นๆ การผสานรวม Magento API ที่แข็งแกร่งช่วยให้การซิงโครไนซ์ผลิตภัณฑ์, การประมวลผลคำสั่งซื้อ, การอัปเดตสินค้าคงคลัง และการจัดการข้อมูลลูกค้าเป็นไปโดยอัตโนมัติ

คู่มือนี้จะแนะนำคุณตลอดกระบวนการผสานรวม Magento 2 API ที่สมบูรณ์ คุณจะได้เรียนรู้การยืนยันตัวตนแบบ OAuth 1.0a และ token, REST/SOAP/GraphQL endpoint, การจัดการผลิตภัณฑ์และคำสั่งซื้อ, webhooks, การพัฒนา API แบบกำหนดเอง และกลยุทธ์การปรับใช้สำหรับการใช้งานจริง เมื่ออ่านจบ คุณจะมีการผสานรวม Magento ที่พร้อมใช้งานจริง

💡 เคล็ดลับ: Apidog ช่วยให้การทดสอบ/จำลอง API Magento ของคุณเป็นเรื่องง่าย สามารถตรวจสอบ authentication, การตอบกลับ API, นำเข้าข้อมูลจำเพาะ, แชร์กับทีม และอื่นๆ ได้ในที่เดียว

Magento 2 API คืออะไร?

Magento 2 มี API สามประเภทสำหรับการเข้าถึงข้อมูลอีคอมเมิร์ซ:

  • REST API: ใช้ JSON สำหรับเว็บและแอปพลิเคชันมือถือ
  • SOAP API: ใช้ XML สำหรับการผสานรวมระดับองค์กร
  • GraphQL: ใช้การสอบถามข้อมูลเพื่อแอปพลิเคชันส่วนหน้าที่มีประสิทธิภาพ

API ครอบคลุมการเข้าถึง:

  • ผลิตภัณฑ์, หมวดหมู่, สินค้าคงคลัง
  • คำสั่งซื้อ, ใบแจ้งหนี้, การจัดส่ง
  • ลูกค้าและกลุ่มลูกค้า
  • ตะกร้าสินค้า, การชำระเงิน
  • โปรโมชัน, กฎการกำหนดราคา
  • หน้าและบล็อก CMS
  • การตั้งค่าร้านค้า

คุณสมบัติหลัก

คุณสมบัติ คำอธิบาย
หลายโปรโตคอล REST, SOAP, GraphQL
OAuth 1.0a การเข้าถึงจากบุคคลที่สามที่ปลอดภัย
การยืนยันตัวตนด้วยโทเค็น โทเค็นผู้ดูแลระบบและโทเค็นการผสานรวม
Webhooks การดำเนินการแบบอะซิงโครนัสผ่านคิว
การจำกัดอัตรา กำหนดค่าได้ต่อการติดตั้ง
Custom Endpoints ขยายด้วย API ที่กำหนดเอง
หลายร้านค้า API เดียว, มุมมองร้านค้าหลายมุมมอง

การเปรียบเทียบ API

ประเภท API โปรโตคอล กรณีการใช้งาน
REST JSON แอปมือถือ, การผสานรวมทั่วไป
SOAP XML ระบบองค์กร (SAP, Oracle)
GraphQL GraphQL หน้าร้าน, PWA

เวอร์ชันของ Magento

เวอร์ชัน สถานะ สิ้นสุดการสนับสนุน
Magento 2.4.x ปัจจุบัน ใช้งานอยู่
Adobe Commerce 2.4.x ปัจจุบัน ใช้งานอยู่
Magento 1.x EOL มิ.ย. 2020 (ไม่ควรใช้)

เริ่มต้นใช้งาน: การตั้งค่าการยืนยันตัวตน

ขั้นตอนที่ 1: สร้างบัญชีผู้ดูแลระบบหรือการผสานรวม

  1. เข้าสู่ระบบ Magento Admin Panel
  2. ไปที่ System > Permissions > All Users
  3. สร้างผู้ใช้ผู้ดูแลระบบ (ใช้สำหรับโทเค็นผู้ดูแลระบบ)
  4. หรือไปที่ System > Extensions > Integrations เพื่อสร้าง integration ใหม่ (ใช้สำหรับ OAuth)

ขั้นตอนที่ 2: เลือกวิธีการยืนยันตัวตน

วิธีการ เหมาะสำหรับ อายุการใช้งานโทเค็น
โทเค็นผู้ดูแลระบบ การผสานรวมภายใน กำหนดค่าได้ (เริ่มต้น 4 ชม.)
โทเค็นการผสานรวม แอปบุคคลที่สาม จนกว่าจะเพิกถอน
OAuth 1.0a แอปในตลาดสาธารณะ จนกว่าจะเพิกถอน
โทเค็นลูกค้า แอปสำหรับลูกค้า กำหนดค่าได้

ขั้นตอนที่ 3: รับโทเค็นผู้ดูแลระบบ (วิธีที่ง่ายที่สุด)

const MAGENTO_BASE_URL = process.env.MAGENTO_BASE_URL;
const MAGENTO_ADMIN_USERNAME = process.env.MAGENTO_ADMIN_USERNAME;
const MAGENTO_ADMIN_PASSWORD = process.env.MAGENTO_ADMIN_PASSWORD;

const getAdminToken = async () => {
  const response = await fetch(`${MAGENTO_BASE_URL}/rest/V1/integration/admin/token`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      username: MAGENTO_ADMIN_USERNAME,
      password: MAGENTO_ADMIN_PASSWORD
    })
  });

  if (!response.ok) {
    throw new Error('Invalid admin credentials');
  }

  const token = await response.text();
  return token;
};

// ใช้งาน
const token = await getAdminToken();
console.log(`Admin token: ${token}`);
// เก็บรักษา token อย่างปลอดภัย
Enter fullscreen mode Exit fullscreen mode

หมายเหตุความปลอดภัย: token ควรเก็บใน .env หรือ vault

# .env file
MAGENTO_BASE_URL="https://store.example.com"
MAGENTO_ADMIN_USERNAME="api_user"
MAGENTO_ADMIN_PASSWORD="secure_password_here"
MAGENTO_ACCESS_TOKEN="obtained_via_auth"
Enter fullscreen mode Exit fullscreen mode

ขั้นตอนที่ 4: สร้างการผสานรวม (แนะนำสำหรับบุคคลที่สาม)

  1. ไปที่ System > Extensions > Integrations
  2. คลิก Add New Integration
  3. กรอกชื่อ, อีเมล, Callback URL, Identity Link URL (ถ้ามี)
  4. ตั้งค่า API Permissions เลือกเฉพาะสิทธิ์ที่ต้องการ (เช่น ผลิตภัณฑ์, คำสั่งซื้อ, ลูกค้า)
  5. คลิก Save และ Activate
  6. คัดลอก Access Token และ Token Secret

ขั้นตอนที่ 5: รับโทเค็นลูกค้า

const getCustomerToken = async (email, password) => {
  const response = await fetch(`${MAGENTO_BASE_URL}/rest/V1/integration/customer/token`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      username: email,
      password: password
    })
  });

  if (!response.ok) {
    throw new Error('Invalid customer credentials');
  }

  const token = await response.text();
  return token;
};

// ใช้งาน
const customerToken = await getCustomerToken('customer@example.com', 'password123');
Enter fullscreen mode Exit fullscreen mode

ขั้นตอนที่ 6: เรียกใช้ API ที่มีการยืนยันตัวตน

const magentoRequest = async (endpoint, options = {}) => {
  const token = await getAdminToken(); // หรือดึง token จาก storage

  const response = await fetch(`${MAGENTO_BASE_URL}/rest${endpoint}`, {
    ...options,
    headers: {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json',
      ...options.headers
    }
  });

  if (!response.ok) {
    const error = await response.json();
    throw new Error(`Magento API Error: ${error.message}`);
  }

  return response.json();
};

// ใช้งาน
const products = await magentoRequest('/V1/products');
console.log(`พบ ${products.items.length} ผลิตภัณฑ์`);
Enter fullscreen mode Exit fullscreen mode

การจัดการผลิตภัณฑ์

การดึงข้อมูลผลิตภัณฑ์

const getProducts = async (filters = {}) => {
  const params = new URLSearchParams();

  if (filters.search) {
    params.append('searchCriteria[filterGroups][0][filters][0][field]', 'sku');
    params.append('searchCriteria[filterGroups][0][filters][0][value]', `%${filters.search}%`);
    params.append('searchCriteria[filterGroups][0][filters][0][conditionType]', 'like');
  }
  if (filters.priceFrom) {
    params.append('searchCriteria[filterGroups][1][filters][0][field]', 'price');
    params.append('searchCriteria[filterGroups][1][filters][0][value]', filters.priceFrom);
    params.append('searchCriteria[filterGroups][1][filters][0][conditionType]', 'gteq');
  }

  params.append('searchCriteria[pageSize]', filters.limit || 20);
  params.append('searchCriteria[currentPage]', filters.page || 1);

  const response = await magentoRequest(`/V1/products?${params.toString()}`);
  return response;
};

// ตัวอย่างการใช้งาน
const products = await getProducts({ search: 'shirt', priceFrom: 20, limit: 50 });
products.items.forEach(product => {
  console.log(`${product.sku}: ${product.name} - $${product.price}`);
});
Enter fullscreen mode Exit fullscreen mode

การดึงข้อมูลผลิตภัณฑ์เดียว

const getProduct = async (sku) => {
  const response = await magentoRequest(`/V1/products/${sku}`);
  return response;
};

// ตัวอย่าง
const product = await getProduct('TSHIRT-001');
console.log(`Name: ${product.name}`);
console.log(`Price: $${product.price}`);
console.log(`Stock: ${product.extension_attributes?.stock_item?.qty}`);
Enter fullscreen mode Exit fullscreen mode

การสร้างผลิตภัณฑ์

const createProduct = async (productData) => {
  const product = {
    product: {
      sku: productData.sku,
      name: productData.name,
      attribute_set_id: productData.attributeSetId || 4,
      type_id: 'simple',
      price: productData.price,
      status: productData.status || 1,
      visibility: productData.visibility || 4,
      weight: productData.weight || 1,
      extension_attributes: {
        stock_item: {
          qty: productData.qty || 0,
          is_in_stock: productData.qty > 0
        }
      },
      custom_attributes: [
        { attribute_code: 'description', value: productData.description },
        { attribute_code: 'short_description', value: productData.shortDescription },
        { attribute_code: 'color', value: productData.color },
        { attribute_code: 'size', value: productData.size }
      ]
    }
  };

  const response = await magentoRequest('/V1/products', {
    method: 'POST',
    body: JSON.stringify(product)
  });

  return response;
};

// ตัวอย่าง
const newProduct = await createProduct({
  sku: 'TSHIRT-NEW-001',
  name: 'Premium Cotton T-Shirt',
  price: 29.99,
  qty: 100,
  description: 'High-quality cotton t-shirt',
  shortDescription: 'Premium cotton tee',
  color: 'Blue',
  size: 'M'
});
console.log(`Product created: ${newProduct.id}`);
Enter fullscreen mode Exit fullscreen mode

การอัปเดตผลิตภัณฑ์

const updateProduct = async (sku, updates) => {
  const product = {
    product: {
      sku: sku,
      ...updates
    }
  };

  const response = await magentoRequest(`/V1/products/${sku}`, {
    method: 'PUT',
    body: JSON.stringify(product)
  });

  return response;
};

// ตัวอย่าง: อัปเดตราคาและ stock
await updateProduct('TSHIRT-001', {
  price: 24.99,
  extension_attributes: {
    stock_item: {
      qty: 150,
      is_in_stock: true
    }
  }
});
Enter fullscreen mode Exit fullscreen mode

การลบผลิตภัณฑ์

const deleteProduct = async (sku) => {
  await magentoRequest(`/V1/products/${sku}`, {
    method: 'DELETE'
  });

  console.log(`Product ${sku} deleted`);
};
Enter fullscreen mode Exit fullscreen mode

ประเภทผลิตภัณฑ์

ประเภท คำอธิบาย กรณีการใช้งาน
Simple SKU เดียว, ไม่มีตัวเลือก สินค้าทั่วไป
Configurable ผลิตภัณฑ์หลัก + ตัวเลือกย่อย สี/ขนาด
Grouped กลุ่ม simple หลายตัว ชุดสินค้า
Virtual ไม่ใช่กายภาพ บริการ, ดาวน์โหลด
Bundle ชุดสินค้าปรับแต่งได้ สร้างชุดเอง
Downloadable ดิจิทัล E-books, ซอฟต์แวร์

การจัดการคำสั่งซื้อ

การดึงข้อมูลคำสั่งซื้อ

const getOrders = async (filters = {}) => {
  const params = new URLSearchParams();

  if (filters.status) {
    params.append('searchCriteria[filterGroups][0][filters][0][field]', 'status');
    params.append('searchCriteria[filterGroups][0][filters][0][value]', filters.status);
    params.append('searchCriteria[filterGroups][0][filters][0][conditionType]', 'eq');
  }
  if (filters.dateFrom) {
    params.append('searchCriteria[filterGroups][1][filters][0][field]', 'created_at');
    params.append('searchCriteria[filterGroups][1][filters][0][value]', filters.dateFrom);
    params.append('searchCriteria[filterGroups][1][filters][0][conditionType]', 'gteq');
  }

  params.append('searchCriteria[pageSize]', filters.limit || 20);
  params.append('searchCriteria[currentPage]', filters.page || 1);

  const response = await magentoRequest(`/V1/orders?${params.toString()}`);
  return response;
};

// ตัวอย่าง: ดึงคำสั่งซื้อ pending 7 วันล่าสุด
const orders = await getOrders({
  status: 'pending',
  dateFrom: '2026-03-18 00:00:00',
  limit: 50
});
orders.items.forEach(order => {
  console.log(`Order #${order.increment_id}: ${order.customer_email} - $${order.grand_total}`);
});
Enter fullscreen mode Exit fullscreen mode

การดึงข้อมูลคำสั่งซื้อเดียว

const getOrder = async (orderId) => {
  const response = await magentoRequest(`/V1/orders/${orderId}`);
  return response;
};

// ตัวอย่าง
const order = await getOrder(12345);
console.log(`Order #${order.increment_id}`);
console.log(`Status: ${order.status}`);
console.log(`Total: $${order.grand_total}`);
order.items.forEach(item => {
  console.log(`  - ${item.name} x ${item.qty_ordered}`);
});
Enter fullscreen mode Exit fullscreen mode

ลำดับสถานะคำสั่งซื้อ

pending → processing → complete
        → canceled
        → on_hold
        → payment_review
Enter fullscreen mode Exit fullscreen mode

การอัปเดตสถานะคำสั่งซื้อ

const updateOrderStatus = async (orderId, newStatus) => {
  // หากต้องการเปลี่ยนสถานะโดยตรง ต้องใช้ custom endpoint
  // วิธีมาตรฐาน: cancel, hold, unhold

  // cancel
  await magentoRequest(`/V1/orders/${orderId}/cancel`, { method: 'POST' });

  // hold
  await magentoRequest(`/V1/orders/${orderId}/hold`, { method: 'POST' });

  // unhold
  await magentoRequest(`/V1/orders/${orderId}/unhold`, { method: 'POST' });
};
Enter fullscreen mode Exit fullscreen mode

การสร้างใบแจ้งหนี้

const createInvoice = async (orderId, items = [], notify = true, appendComment = false, comment = null) => {
  const invoice = {
    capture: true,
    last: true,
    items: items
  };

  if (comment) {
    invoice.comment = comment;
    invoice.notify_customer = notify ? 1 : 0;
    invoice.append_comment = appendComment ? 1 : 0;
  }

  const response = await magentoRequest(`/V1/order/${orderId}/invoice`, {
    method: 'POST',
    body: JSON.stringify(invoice)
  });

  return response;
};

// ตัวอย่าง
const invoiceId = await createInvoice(12345, [], true, false, 'ขอบคุณสำหรับการสั่งซื้อ!');
console.log(`Invoice created: ${invoiceId}`);
Enter fullscreen mode Exit fullscreen mode

การสร้างการจัดส่ง

const createShipment = async (orderId, items = [], notify = true, appendComment = false, comment = null, tracks = []) => {
  const shipment = {
    items: items,
    notify: notify ? 1 : 0,
    append_comment: appendComment ? 1 : 0,
    comment: comment,
    tracks: tracks
  };

  const response = await magentoRequest(`/V1/order/${orderId}/ship`, {
    method: 'POST',
    body: JSON.stringify(shipment)
  });

  return response;
};

// ตัวอย่าง: shipment พร้อม tracking
const shipmentId = await createShipment(12345, [], true, false, 'สินค้าถูกจัดส่งแล้ว!', [
  {
    track_number: '1Z999AA10123456784',
    title: 'Tracking Number',
    carrier_code: 'ups'
  }
]);
console.log(`Shipment created: ${shipmentId}`);
Enter fullscreen mode Exit fullscreen mode

การจัดการลูกค้า

การดึงข้อมูลลูกค้า

const getCustomers = async (filters = {}) => {
  const params = new URLSearchParams();

  if (filters.email) {
    params.append('searchCriteria[filterGroups][0][filters][0][field]', 'email');
    params.append('searchCriteria[filterGroups][0][filters][0][value]', filters.email);
    params.append('searchCriteria[filterGroups][0][filters][0][conditionType]', 'eq');
  }

  params.append('searchCriteria[pageSize]', filters.limit || 20);

  const response = await magentoRequest(`/V1/customers/search?${params.toString()}`);
  return response;
};

// ตัวอย่าง
const customers = await getCustomers({ email: 'customer@example.com' });
customers.items.forEach(customer => {
  console.log(`${customer.firstname} ${customer.lastname} - ${customer.email}`);
});
Enter fullscreen mode Exit fullscreen mode

การสร้างลูกค้า

const createCustomer = async (customerData) => {
  const customer = {
    customer: {
      websiteId: customerData.websiteId || 1,
      email: customerData.email,
      firstname: customerData.firstname,
      lastname: customerData.lastname,
      middlename: customerData.middlename || '',
      gender: customerData.gender || 0,
      store_id: customerData.storeId || 0,
      extension_attributes: {
        is_subscribed: customerData.subscribed || false
      }
    },
    password: customerData.password
  };

  const response = await magentoRequest('/V1/customers', {
    method: 'POST',
    body: JSON.stringify(customer)
  });

  return response;
};

// ตัวอย่าง
const newCustomer = await createCustomer({
  email: 'newcustomer@example.com',
  firstname: 'John',
  lastname: 'Doe',
  password: 'SecurePass123!',
  subscribed: true
});
console.log(`Customer created: ID ${newCustomer.id}`);
Enter fullscreen mode Exit fullscreen mode

การจัดการสินค้าคงคลัง (MSI)

การดึงข้อมูลสถานะสต็อก

const getStockStatus = async (sku) => {
  const response = await magentoRequest(`/V1/products/${sku}/stockItems/1`);
  return response;
};

// ตัวอย่าง
const stock = await getStockStatus('TSHIRT-001');
console.log(`Qty: ${stock.qty}`);
console.log(`In Stock: ${stock.is_in_stock}`);
console.log(`Min Qty: ${stock.min_qty}`);
Enter fullscreen mode Exit fullscreen mode

การอัปเดตสต็อก

const updateStock = async (sku, qty, isInStock = null) => {
  const stockItem = {
    stockItem: {
      qty: qty,
      is_in_stock: isInStock !== null ? isInStock : qty > 0
    }
  };

  const response = await magentoRequest(`/V1/products/${sku}/stockItems/1`, {
    method: 'PUT',
    body: JSON.stringify(stockItem)
  });

  return response;
};

// ตัวอย่าง
await updateStock('TSHIRT-001', 100, true);
Enter fullscreen mode Exit fullscreen mode

Webhooks และการดำเนินการแบบอะซิงโครนัส

การตั้งค่า Webhooks

Magento ไม่มี webhooks แบบ native สำหรับ event เช่น คำสั่งซื้อใหม่ ต้องใช้วิธีเหล่านี้:

// 1. Poll endpoint orders เป็นระยะ
const pollNewOrders = async (lastOrderId) => {
  const orders = await getOrders({
    dateFrom: new Date().toISOString()
  });

  const newOrders = orders.items.filter(o => o.id > lastOrderId);
  return newOrders;
};

// 2. ใช้ Adobe I/O Events (สำหรับ Adobe Commerce)
// ตั้งค่าใน Adobe Developer Console

// 3. เขียน custom webhook module
// ดู doc: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/message-queues/message-queues.html
Enter fullscreen mode Exit fullscreen mode

การจำกัดอัตรา (Rate Limiting)

การทำความเข้าใจการจำกัดอัตรา

  • ค่าเริ่มต้น: ไม่มีขีดจำกัด (แต่แนะนำให้ตั้งค่า)
  • แนะนำ: 100-1000 requests/นาที
  • ตั้งค่าที่ Admin: Stores > Configuration > Services > Web API > Security

การนำการจัดการการจำกัดอัตราไปใช้

const makeRateLimitedRequest = async (endpoint, options = {}, maxRetries = 3) => {
  for (let attempt = 1; attempt <= maxRetries; attempt++) {
    try {
      const response = await magentoRequest(endpoint, options);
      return response;
    } catch (error) {
      if (error.message.includes('429') && attempt < maxRetries) {
        const delay = Math.pow(2, attempt) * 1000;
        await new Promise(resolve => setTimeout(resolve, delay));
      } else {
        throw error;
      }
    }
  }
};
Enter fullscreen mode Exit fullscreen mode

รายการตรวจสอบสำหรับการปรับใช้จริง

ก่อนใช้งานจริง:

  • [ ] ใช้โทเค็นการผสานรวม (ไม่ใช้ข้อมูลผู้ดูแลระบบ)
  • [ ] จัดเก็บโทเค็นในที่ปลอดภัย (เช่น DB เข้ารหัส)
  • [ ] ใช้ rate limiting และ queue
  • [ ] จัดการข้อผิดพลาดรอบด้าน
  • [ ] บันทึกทุก API call
  • [ ] สร้าง webhook (หรือ polling/Adobe I/O) สำหรับ event สำคัญ
  • [ ] ทดสอบกับข้อมูลจริง
  • [ ] มี retry logic สำหรับ request ที่ fail

กรณีการใช้งานจริง

การผสานรวม ERP

  • ปัญหา: ต้องอัปเดตสต็อกระหว่าง ERP กับ Magento ด้วยมือ
  • แนวทาง: ใช้ API sync 2 ทาง ทุก 15 นาที
  • ผลลัพธ์: ข้อมูลสต็อก real-time, ไม่เกิด oversell

แอปพลิเคชันมือถือ

  • ปัญหา: ต้องการประสบการณ์ native
  • แนวทาง: ใช้ GraphQL ดึงข้อมูล, REST สำหรับ checkout
  • ผลลัพธ์: conversion rate มือถือ +40%

สรุป

Magento 2 API มอบความสามารถอีคอมเมิร์ซครบถ้วน ให้คุณ:

  • เลือก REST, SOAP หรือ GraphQL ได้ตามความเหมาะสม
  • ใช้ token authentication เพื่อความปลอดภัย
  • จัดการ CRUD สินค้า, คำสั่งซื้อ, ลูกค้า ได้เต็มระบบ
  • จัดการสินค้าคงคลังผ่าน MSI
  • ตั้งค่า rate limit ได้ตามต้องการ
  • Apidog ช่วยให้งานทดสอบและ teamwork ง่ายขึ้นมาก

ส่วนคำถามที่พบบ่อย (FAQ)

ฉันจะยืนยันตัวตนกับ Magento API ได้อย่างไร?

ใช้โทเค็นผู้ดูแลระบบสำหรับการผสานรวมภายใน หรือสร้าง Integration ใน System > Extensions สำหรับ OAuth ส่วนโทเค็นลูกค้าใช้กับแอปที่ลูกค้าใช้งาน

ความแตกต่างระหว่าง REST และ GraphQL ใน Magento คืออะไร?

REST รองรับ CRUD เต็มรูปแบบ, GraphQL เหมาะกับ frontend ที่ต้องการ query ข้อมูลแบบยืดหยุ่นและประหยัด bandwidth

ฉันจะสร้างผลิตภัณฑ์ผ่าน API ได้อย่างไร?

ส่ง POST ไปยัง /V1/products พร้อมข้อมูล SKU, ชื่อ, ราคา และ stock_item ใน extension_attributes

ฉันสามารถรับ webhooks สำหรับคำสั่งซื้อใหม่ได้หรือไม่?

Magento ไม่มี webhook ในตัว ให้ใช้ polling, Adobe I/O Events (เฉพาะ Adobe Commerce) หรือเขียน module เอง

ฉันจะอัปเดตปริมาณสต็อกได้อย่างไร?

ส่ง PUT ไปที่ /V1/products/{sku}/stockItems/1 พร้อมข้อมูล qty และ is_in_stock

Top comments (0)