Integrations

Custom Backends

Edit this page

If you aren't using Shopify or Square, or if you need to pull data from multiple sources, you can easily implement a Custom Backend.

Implementation

To create a custom backend, you simply need to implement one or more of the core interfaces: ProductBackend, CartBackend, or OrderBackend.

Example: MySQL/Postgres Backend

1import { ProductBackend, Product } from '@commercegateway/commerce-gateway';
2 
3class MyDatabaseBackend implements ProductBackend {
4 async searchProducts(query: string): Promise<ProductSearchResult> {
5 const results = await db.products.findMany({
6 where: { name: { contains: query } }
7 });
8
9 return {
10 products: results.map(this.mapToProduct),
11 total: results.length,
12 hasMore: false,
13 };
14 }
15 
16 async getProductDetails(id: string): Promise<Product | null> {
17 const product = await db.products.findUnique({ where: { id } });
18 return product ? this.mapToProduct(product) : null;
19 }
20 
21 private mapToProduct(raw: any): Product {
22 return {
23 id: raw.id,
24 name: raw.title,
25 price: { amount: raw.price, currency: 'USD' },
26 "cmt">// ...
27 };
28 }
29}

Using your Backend

Once implemented, pass your custom class to the gateway:

1const gateway = new LLMGateway({
2 backends: {
3 products: new MyDatabaseBackend(),
4 }
5});

Hybrid Backends

You can also mix and match. For example, use Shopify for product data but a Custom API for complex loyalty points or custom fulfillment rules.

1const gateway = new LLMGateway({
2 backends: {
3 products: new ShopifyBackend(...),
4 cart: new MyCustomLoyaltyCartBackend(...),
5 }
6});

Best Practices

  • Timeout Management: Ensure your custom backend methods have sensible timeouts to prevent blocking the LLM interaction.
  • Validation: Use Zod within your backend to validate data coming from your internal services.
  • Error Mapping: Map your internal database errors to the gateway's Standard Error Types.