> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

## Resource: Add Self-Managed Service Plugin Extensions with the SDK

## Article: Add Self-hosted Service Plugin Extensions with the SDK

## Article Link: https://dev.wix.com/docs/build-apps/develop-your-app/develop-a-self-managed-app/supported-extensions/backend-extensions/service-plugins/add-self-managed-service-plugin-extensions-with-the-sdk.md

## Article Content:

# Add Self-Managed Service Plugin Extensions with the SDK

Add a [service plugin](https://dev.wix.com/docs/build-apps/develop-your-app/extensions/backend-extensions/service-plugins/about-service-plugin-extensions.md) (formerly SPI) extension to your app in your [app's dashboard](https://manage.wix.com/account/custom-apps) to extend the functionality of a Wix site.  

With service plugins, your app can:

+ **Inject custom logic into existing Wix app flows:** For example, a typical eCommerce checkout process does not allow a site owner to add additional fees that are unrelated to specific line items. Your app can use the [Additional Fees service plugin](https://dev.wix.com/docs/sdk/backend-modules/ecom/service-plugins/additional-fees/introduction.md) to add extra fees, like gift wrap charges or a fee for fragile items, to the Wix checkout flow.
+ **Introduce entirely new flows to Wix sites:** For example, when a site owner sets up a Wix store, there are a limited number of payment and shipping rates service providers to choose from. Your app can use the [Shipping Rates service plugin](https://dev.wix.com/docs/sdk/backend-modules/ecom/service-plugins/shipping-rates/introduction.md) to offer additional 3rd-party shipping rates providers currently unavailable to Wix sites.

If you prefer to host your app on Wix, you can [add a service plugin extension with the CLI](https://dev.wix.com/docs/build-apps/develop-your-app/develop-an-app-with-the-cli/supported-extensions/backend/service-plugins/add-service-plugin-extensions-with-the-wix-cli.md).

Follow these steps to implement a self-managed service plugin with the [Wix JavaScript SDK](https://dev.wix.com/docs/sdk.md):

## Step 1 | Add a service plugin extension to your app

Add a service plugin extension to your app in your app's dashboard as follows:

1. Select an app from the [Custom Apps page](https://manage.wix.com/account/custom-apps) in your Wix Studio workspace.

1. Go to the Extensions page, and click **+ Create Extension**.

   ![Create extension](https://wixmp-833713b177cebf373f611808.wixmp.com/images/72e71b0414df528b52e1b149f8b15114.png)

1. Filter by tag, or search to find the extension you want to add.

1. Select the relevant extension and click **+ Create**.

1. In the JSON editor, configure the parameters by referencing the **Documentation** section on the right side of the page. For each parameter, add the parameter name and value in the JSON editor.

   ![screenshot of json editor](https://wixmp-833713b177cebf373f611808.wixmp.com/images/8373341d8ee43e8b34c20e09776395fa.png)

1. Click **Save**.  

## Step 2 | Retrieve your app's credentials

To implement a self-managed service plugin, retrieve the following credentials from your app's dashboard:

+ **App ID**
+ **Public key**

To retrieve your credentials:

1. In your [app dashboard](https://manage.wix.com/account/custom-apps), click the **More Actions** menu on the top right.
1. Select **View ID & Keys**.
1. Click **Show** and copy the **App ID** and **Public Key**.

> **Note:** If your handler needs to make outbound calls to Wix APIs, you also need the **App Secret Key** from the same screen. The App Secret Key isn't required to receive and verify inbound SPI calls.

## Step 3 | Import modules and create a client

In your app code, import the Wix client and service plugin modules you need, then call [`createClient()`](https://dev.wix.com/docs/sdk/core-modules/sdk/wix-client.md#createclient) with the [`AppStrategy`](https://dev.wix.com/docs/sdk/core-modules/sdk/app-strategy.md) auth strategy to create a client that receives and routes inbound SPI calls:

```js
import { createClient, AppStrategy } from '@wix/sdk';
import { <service-plugin> } from '@wix/<service-module>/service-plugins';

const wixClient = createClient({
  auth: AppStrategy({
    appId: <YOUR_APP_ID>,
    publicKey: <YOUR_PUBLIC_KEY>,
  }),
  modules: { <service-plugin> }
});
```

Make sure you:

+ Import the correct module for your service plugin and pass it in `modules` when creating the client.
+ Provide the App ID and Public Key you retrieved in the previous step.

> **Note:** This client handles inbound SPI calls only. If your handler needs to make outbound calls to Wix APIs, create a separate client per request using your App Secret Key and `metadata.instanceId` from the inbound payload.


## Step 4 | Define handler functions

Now it's time to add your custom business logic. Do this by calling `provideHandlers()` and passing it an object containing handler functions for each service plugin functionality you need:

```js
  wixClient.<service-plugin>.provideHandlers({
    <service-plugin-function-1>: async (payload) => {

      const { request, metadata } = payload;

      // Add your logic here

    },
    <service-plugin-function-2>: async (payload) => {

      const { request, metadata } = payload;

      // Add your logic here
    
    }
  });
```

For example, to implement the [Shipping Rates](https://dev.wix.com/docs/api-reference/business-solutions/e-commerce/extensions/shipping-rates/shipping-rates-integration-service-plugin/introduction.md) service plugin, define the [`getShippingRates()`](https://dev.wix.com/docs/api-reference/business-solutions/e-commerce/extensions/shipping-rates/shipping-rates-integration-service-plugin/get-shipping-rates.md) handler function as follows:

```js
  wixClient.shippingRates.provideHandlers({
    getShippingRates: async (payload) => {

      const { request, metadata } = payload;

      // Add your logic here

    }
  });
```

When a site action triggers one of these functions, Wix passes the function a `payload` object containing the following properties:

+ `request`: Details related to the specific service and action.
+ `metadata`: Information about the context of the request, such as the App Instance ID, site currency, language, and identity of the user who triggered the request.

To find the handler functions a service plugin supports, and the precise structure of the `request` and `metadata` objects for each handler function, see the [reference documentation](https://dev.wix.com/docs/sdk.md).

## Step 5 | Expose an endpoint

Define a route to handle `POST` requests from Wix. In your route handler, call `wixClient.servicePlugins.process()` and pass it the request URL and raw body. This method takes care of the following for you:

+ It decodes the JWT-encrypted requests.
+ Based on the sub-path of the request, it triggers a call to the correct handler function you defined with `provideHandlers()` and passes it the request payload.

For example, if you are using [Express](https://expressjs.com/), implement the route as follows:

```js
import express from 'express';

const app = express();

// Wix sends a signed JWT as plain text. Parse the body as raw text before processing.
app.post(
  '/*splat',
  express.text(),
  async (req, res) => {
    try {
      const result = await wixClient.servicePlugins.process({
        url: req.originalUrl,
        body: req.body,
      });
      res.json(result);
    } catch (err) {
      res.status(500).json({ error: err.message });
    }
  },
);
```

## Full code example

Here's an example of implementing this code for a shipping rates service plugin:

```js
import express from 'express';
import { createClient, AppStrategy } from '@wix/sdk';
import { shippingRates } from '@wix/ecom/service-plugins';

const app = express();

const wixClient = createClient({
  auth: AppStrategy({
    appId: <YOUR_APP_ID>,
    publicKey: <YOUR_APP_PUBLIC_KEY>,
  }),
  modules: { shippingRates },
});

wixClient.shippingRates.provideHandlers({
  getShippingRates: async (payload) => {
    const { request, metadata } = payload;

    // Add your logic here
  },
});

app.post(
  '/*splat',
  express.text(),
  async (req, res) => {
    try {
      const result = await wixClient.servicePlugins.process({
        url: req.originalUrl,
        body: req.body,
      });
      res.json(result);
    } catch (err) {
      res.status(500).json({ error: err.message });
    }
  },
);

app.listen(3000);
```
  
## See also

+ [About service plugin extensions](https://dev.wix.com/docs/build-apps/develop-your-app/extensions/backend-extensions/service-plugins/about-service-plugin-extensions.md)
+ [Add a service plugin with the CLI](https://dev.wix.com/docs/build-apps/develop-your-app/develop-an-app-with-the-cli/supported-extensions/backend/service-plugins/add-service-plugin-extensions-with-the-wix-cli.md)