# Preview and Save Template

Once content has been added to a template, it's not automatically available for use. Until save template action is called, you can create products from the template and they won't contain pages that are still being edited.

## Save Template

Use the [PUT Save template](https://developer.smartbear.com/portal/default/swaggerhub-portal-api#/Templates%20%3A%3A%20Save/saveTemplate) endpoint to save newly added content to a template.

### Sample request 

```text
curl -X PUT "https://api.portal.swaggerhub.com/v1/templates/<TEMPLATE-ID>/saved" \
-H "Authorization: Bearer YOUR_SWAGGER_APIKEY" \
-H "Content-Type: application/json" 
```

In case of a processing error, the response with validation message is returned providing information of the error type and ID of each **template** **table-of-contents** (template TOC) entry that caused the issue. 

### Sample Response

```text
{
  "validationMessages": [
    {
      "level": "error",
      "message": "The product failed to publish due to errors.",
      "errorCode": "INVALID_API",
      "tableOfContentsId": "f91f8be2-ccbc-483a-93a7-a692763f79a5"
    }
  ]
}
```

Possible validation error code are explained [here](https://developer.smartbear.com/portal/default/publish-product#possible-error-types-and-their-explanation).

## Unsaved changes

To retrieve the changes made to a template since the last save, you can use the [GET Get unsaved changes](https://developer.smartbear.com/portal/default/swaggerhub-portal-api#/Templates%20%3A%3A%20Save/getUnsavedChanges) endpoint:

### Sample Request

```text
curl -X GET "https://api.portal.swaggerhub.com/v1/templates/<TEMPLATE-ID>/saved/changes" \
-H "Authorization: Bearer YOUR_SWAGGER_APIKEY" \
-H "Content-Type: application/json"
```

### Sample Response

```text
{
  "added": [
    {
      "path": "doc/new-feature",
      "tableOfContentsId": "12345678-1234-1234-1234-123456789abc",
      "title": "New Feature Documentation",
      "type": "markdown"
    }
  ],
  "modified": [
    {
      "path": "doc/updated-feature",
      "tableOfContentsId": "87654321-4321-4321-4321-cba987654321",
      "title": "Updated Feature Documentation",
      "type": "html"
    }
  ],
  "removed": [],
  "missing": [],
  "reordered": false
}
```

> 🚧 **Important:** If any of you APIs appears as missing, it means it's inaccessible in Swagger Studio and must be unlinked to preview or publish the product. Possible causes: 
> 
> -   API renamed in Swagger Studio
> -   API deleted from Swagger Studio
> -   Provider doesn't have correct permissions to the API
> -   Anything that affects the API URL structure

## Preview Template

It's possible to preview how a product created from template will look like. To do that first use the [PUT Generate template preview](https://developer.smartbear.com/portal/default/swaggerhub-portal-api#/Templates%20%3A%3A%20Preview/generateTemplatePreview) endpoint, then you can check the included content with the [GET Get template preview sections](https://developer.smartbear.com/portal/default/swaggerhub-portal-api#/Templates%20%3A%3A%20Preview/getTemplatePreviewSections) and [GET Get template preview page](https://developer.smartbear.com/portal/default/swaggerhub-portal-api#/Templates%20%3A%3A%20Preview/getTemplatePreviewPage) endpoints.

### Generate Template Preview Request

```text
curl -X PUT "https://api.portal.swaggerhub.com/v1/templates/<TEMPLATE-ID>/preview" \
-H "Authorization: Bearer YOUR_SWAGGER_APIKEY" \
-H "Content-Type: application/json"
```

> **📌 Note:** There is also path parameter `saved` of boolean type possible to add to this query, that defaults to `false`. If set to `true`, the preview of saved template version is generated.

### Generate Template Preview Sample Response

When preview was successfully generated, response contains empty list of validation messages

```text
{
  "validationMessages": []
}
```

### Get Template Preview Sections Sample Request

```text
curl -X GET "https://api.portal.swaggerhub.com/v1/templates/<TEMPLATE-ID>/preview/sections" \
-H "Authorization: Bearer YOUR_SWAGGER_APIKEY"
```

### Sample Response for Get Template Preview Sections

```text
{
  "page": {
    "number": 1,
    "size": 10,
    "totalElements": 1,
    "totalPages": 1
  },
  "items": [
    {
      "title": "Getting Started",
      "slug": "getting-started",
      "tableOfContents": [
        {
          "tableOfContentsId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
          "sourceTemplateTocId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
          "type": "markdown",
          "title": "Getting Started",
          "path": "doc/getting-started",
          "children": []
        }
      ]
    }
  ]
}
```

### Get Template Preview Page Sample Request

```text
curl -X GET "https://api.portal.swaggerhub.com/v1/templates/<TEMPLATE-ID>/preview/page?path=doc/getting-started" \
-H "Authorization: Bearer YOUR_SWAGGER_APIKEY" \
-H "Content-Type: application/json" 
```

> 📝 **Note:** `path` parameter value is required. You can copy its value from **GET Published section** response or combine **section**'s and **table-of-contents**' slugs

### Sample Response for Get Template Preview Page

```text
{
  "content": "Getting Started Welcome to the API documentation. This guide will help you get started with our API.",
  "last-modified": "2023-10-01T12:00:00Z",
  "content-type": "text/html"
}
```
