> 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`).
> - 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: Build a Service Plugin in Studio Two

## Article: Build a Service Plugin in Studio Two

## Article Link: https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-backend/service-plugins/build-a-service-plugin-in-studio-two.md

## Article Content:

# Build a Service Plugin in Studio Two

A [service plugin](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-backend/service-plugins/about-service-plugins.md) is a configuration file plus a handler. You generate the files first, then spend most of your time in the handler.

This article walks through that loop for an Ecom Additional Fees plugin. Other plugin types use the same two files. The handler's functions and return value depend on the type you chose.

Follow the instructions below to:

1. Scaffold the plugin.
2. Keep the configuration.
3. Write the handler.

## Step 1 | Scaffold the plugin

Start from a generated service plugin rather than an empty folder, so the file structure and registration are already in place.

For the full procedure, see [Add a Service Plugin Extension in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-backend/service-plugins/add-a-service-plugin-extension-in-studio-two.md).

You can find the new files under `src/extensions/backend/service-plugins/<plugin-name>/`. You spend most of this article in `<plugin-name>.ts`. For what every file in the folder is for, see [Service Plugin Extension Files and Code](https://dev.wix.com/docs/backend-extensions/service-plugins/service-plugin-extension-files-and-code.md).

## Step 2 | Keep the configuration

`<plugin-name>.extension.ts` is the configuration file. For an Ecom Additional Fees plugin it looks like this:

```ts
import { extensions } from '@wix/custom-extensions';

export default extensions.ecomAdditionalFees({
  id: '6eca2ad6-b311-4d92-92d3-88e4ff9fc021',
  name: 'ecom-additional-fee',
  source: './extensions/backend/service-plugins/ecom-additional-fee/ecom-additional-fee.ts',
});
```

Keep `id` stable. `name` matches the folder name. `source` is the path to the handler, relative to `src/`.

The builder function changes with the plugin type. **Generate** picks the function that matches the type you chose. For every field, see [Service Plugin Extension Files and Code](https://dev.wix.com/docs/backend-extensions/service-plugins/service-plugin-extension-files-and-code.md#configuration-fields).

## Step 3 | Write the handler

`<plugin-name>.ts` is the code Wix calls. A generated Ecom Additional Fees handler returns no fees:

```ts
import { additionalFees } from '@wix/ecom/service-plugins';

additionalFees.provideHandlers({
  calculateAdditionalFees: async ({ request, metadata }) => {
    return {
      additionalFees: [],
    };
  },
});
```

`request` is the cart and checkout state Wix sends. `metadata` includes the call context. Your return value has to match the method's contract or Wix ignores the response.

For example, to return a fee:

1. Open `<plugin-name>.ts`.
2. Replace the empty `additionalFees` array with the fees you want to add.
3. Save the file.

With one fee in the array, the handler looks like this:

```ts
import { additionalFees } from '@wix/ecom/service-plugins';

additionalFees.provideHandlers({
  calculateAdditionalFees: async ({ request, metadata }) => {
    return {
      additionalFees: [
        {
          code: 'wrapping-fee',
          name: 'Wrapping Fee',
          price: '5',
          taxDetails: {
            taxable: true,
          },
        },
      ],
      currency: 'USD',
    };
  },
});
```

`currency` has to match the site's currency. The fields each fee needs, including the name and amount, are on [Calculate Additional Fees](https://dev.wix.com/docs/api-reference/business-solutions/e-commerce/extensions/additional-fees/additional-fees-service-plugin/calculate-additional-fees?apiView=SDK.md).

For any other plugin type, open that plugin in [Available Service Plugins](https://dev.wix.com/docs/backend-extensions/service-plugins/available-service-plugins.md) and use its API reference for the handler functions and the response.

To check the plugin, trigger the Wix flow that calls it. For additional fees, that is a checkout on a site with an eCommerce app installed. See [Add a Service Plugin Extension in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-backend/service-plugins/add-a-service-plugin-extension-in-studio-two.md#step-3--check-the-plugin).

When the plugin behaves the way you want, click **Publish** in the Studio Two editor to ship it with your site.

## See also

- [About Service Plugins](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-backend/service-plugins/about-service-plugins.md)
- [Add a Service Plugin Extension in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-backend/service-plugins/add-a-service-plugin-extension-in-studio-two.md)
- [Service Plugin Extension Files and Code](https://dev.wix.com/docs/backend-extensions/service-plugins/service-plugin-extension-files-and-code.md)
- [Available Service Plugins](https://dev.wix.com/docs/backend-extensions/service-plugins/available-service-plugins.md)
- [Calculate Additional Fees](https://dev.wix.com/docs/api-reference/business-solutions/e-commerce/extensions/additional-fees/additional-fees-service-plugin/calculate-additional-fees?apiView=SDK.md)