Use prisma and access shopify API with extension-only app

I would like to know if is there a way to use prisma database within an extension only app. I want to get certain products info from shopify API and use it to suave data on prisma database

Hallo @Samirjs ,

Yes, you can use Prisma with an extension-only Shopify app to manage your database and handle product information from the Shopify API.
Here some e.g.

  1. Install Prisma CLI and initialize your project.
npm install prisma --save-dev
npx prisma init
  1. Configure your file with your database provider (e.g., PostgreSQL, MySQL, SQLite, etc.).
npx prisma migrate dev --name init
  1. Install the Prisma client in your project.
npm install @prisma/client
  1. Generate the Prisma client.
npx prisma generate
  1. Import the client into your app code.
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
  1. Fetch Data from the Shopify API
    Use Shopify’s Admin API to fetch product information. If you’re building a Shopify app, you can use Shopify’s @shopify/shopify-api library.
import { shopifyApi } from '@shopify/shopify-api';

const shopify = shopifyApi({
  apiKey: process.env.SHOPIFY_API_KEY,
  apiSecretKey: process.env.SHOPIFY_API_SECRET,
  scopes: ['read_products'],
  hostName: process.env.SHOPIFY_HOST_NAME,
});

const session = await shopify.session.customAppSession(
  process.env.SHOPIFY_STORE_NAME,
  process.env.SHOPIFY_ACCESS_TOKEN
);

const client = new shopify.clients.Rest({ session });

const products = await client.get({
  path: 'products',
});
  1. Save Data to Prisma Database
const saveProductsToDatabase = async (products) => {
  for (const product of products) {
    await prisma.product.upsert({
      where: { shopifyId: product.id },
      update: {
        title: product.title,
        price: product.variants[0].price,
      },
      create: {
        shopifyId: product.id,
        title: product.title,
        price: product.variants[0].price,
      },
    });
  }
};

const products = await client.get({ path: 'products' });
await saveProductsToDatabase(products.body.products);

5.Deploy Your App

  • Ensure your Prisma database is deployed to a production-ready environment.

  • Use environment variables to securely store database connection strings and Shopify API credentials.

    With this setup, you can fetch product information from Shopify and store it in your Prisma database for further use

    For more information you can contact me directly vie email and please if i was able to help you please like it mark as solution. Thank you
    Best Regards
    Akshay

1 Like

Do I have to host the app on an external server with this approach? Or shopify will host as it is an extension-only app?

@Samirjs " extension-only " Shopify host the extension only not any app host provided by shopify.