---
title: Extend Trial - Action
description: Extends a subscription trial period for a specific duration.
---

Required for :badge{label="Trial Offer"} to work.

To implement the `Extend Trial` action, you need to implement an endpoint and define `features`

## Prerequisites
::grid{cols="2"}
    ::card-baseline{title="Subscriptions - Controller" to="/integrations/controllers/subscriptions"}
      You must implement a `Subscriptions` controller first.
      ::flex{class="mt-4"}
        :badge{label="Required" color="green"}
      ::
    ::
    ::card-baseline{title="Cancel - Action" to="/integrations/actions/cancel"}
      Trial extension is a part of the Cancel Flow. You must implement the `Cancel` action first.
      ::flex{class="mt-4"}
        :badge{label="Required" color="green"}
      ::
    ::
::

## SDK
If you are using the SDK, you can implement the `Extend Trial` action by following the code example below. You don't need to get into the details of the API endpoints, the SDK will take care of that for you.

::code-group
```typescript [Typescript]
import { Integrator } from '@churnkey/sdk'
import { Subscriptions } from '../controllers/Subscriptions'

export const ExtendTrial = Integrator.ExtendTrial.config({
    Subscriptions: Subscriptions,
    features: {
        enabled: true,
        multiple: true,
        durations: {
            period: true,
            date: true
        }
    },
    async handle(ctx, options) {
        const subscription = await this.subscriptions.retrieve({
            customerId: options.customerId,
            id: options.subscriptionId
        })

        await ctx.db.extendSubscriptionTrial(subscription, {
            duration: options.duration
        })
    }
})
```
::

## Endpoints

### Handle :badge{label="Required" color="green"}

`POST /churnkey/actions/subscription/extend-trial`

This endpoints handles the subscription trial extension. You should find the subscription by `customerId` and `subscriptionId` and extend it's trial.

Options for trial extension, provided in the request body.

::collapsible{name="request body"}
  :field-schema{schema="/types/actions/extend-trial/request.type.json"}
::

::collapsible{name="response"}
    ::tabs
        ::div{label="200"}
        Must return empty response.
        ::
        ::div{label="Error"}
        See [Error Responses](/integrations/general#error-responses).
        ::
    ::
::

::collapsible{name="code example"}
  ::code-group
  ```typescript [Typescript Express]
    app.post('/churnkey/actions/subscription/extend-trial', async (req, res) => {
        const subscription = await db.findSubscription(req.body.customerId, req.body.subscriptionId)
        
        await db.extendSubscriptionTrial(subscription, {
            duration: req.body.duration
        })
        res.send()
    })
  ```
  ::
::

## Features :badge{label="required" color="green"}

Features define which behavior is supported for the `Extend Trial` action. Depending on the features you enabled, requests body will have different options.

For example, if you enable only `period` duration type, the `request.body.duration` will be always of `period` type. If you enable both type, `request.body.duration` can be either of type `period` or `date`.

::collapsible{name="schema"}
  :field-schema{schema="/types/actions/extend-trial/features.type.json"}
::
::collapsible{name="code example"}
  ::code-group
  ```typescript [Typescript]
  export interface Features {
    enabled: boolean
    multiple: boolean
    durations: {
      period: boolean
      date: boolean
    }
  }

  export const features: Features = {
    enabled: true,
    multiple: true,
    durations: {
        period: true,
        date: false // this duration type will be disabled
    }
  }
  ```
::
