To call the service, the access key must be provided in the x-api-key header of the HTTP request. If you are using the GraphQL console, you can view the service’s documentation and schema only after entering an access key. Make sure that the scope of the key allows access to the queries and mutations that you require. For example, to grant the key access to all queries and mutations, the keys’s scope must be set to document-rendering-api:query:* document-rendering-api:mutation:*.
Usage
The Document Rendering Service enables callers to create text or HTML templates. According to their intended usage, templates can be email or document:
Email templates are useful when you need to send email with parameterized content. Specifically, the sendMessage mutation of the Email Service provides the ability to reference a template created previously through the Document Rendering Service. For such templates, you can add parameters both in the subject and body.
Document templates are useful for any other contexts where you need objects with parameterized content, like HTML or text messages for your applications. Such templates do not have a subject, so you can add parameters in the body only.
According to their kind, templates can be plain text or HTML. For HTML templates, you can create separate CSS objects that you can later reference from the template. This makes it possible to apply the same CSS styles to multiple templates. If using an external CSS, you can also override the CSS style locally at the template level if needed.
Defining template parameters
To define parameters within your template, use the double curly braces syntax. For example, the following template defines two parameters, {{name}} and {{email}}:
Hello {{name}},
This is to confirm that your email address {{email}} was successfully subscribed to our newsletter.
You can execute and render the template above with a query like the one below:
{"input":{"template":{"type":"document","contentType":"plain","body":"Hello, {{name}}\n\n. This is to confirm that your email address {{email}} was successfully subscribed to our newsletter."},"params":"{\"name\": \"Leonardo\", \"email\": \"leo@example.org\"}"}}
Notice that the value of params input variable is a stringified JSON object. The JSON object has two attributes that are both strings (name and email), each corresponding to a template variable.
It is also possible to supply objects (rather than string values) as input to the query. The code listing below has the same result, but there are two important differences:
The template parameters are declared as person.name and person.email
The input params value is a nested person object which has the name and email properties.
{"input":{"template":{"type":"document","contentType":"plain","body":"Hello, {{person.name}}\n\n. This is to confirm that your email address {{person.email}} was successfully subscribed to our newsletter."},"params":"{\"person\": {\"name\": \"Leonardo\", \"email\": \"test@example.org\"}}"}}
Queries
css
Returns any CSS objects that match the filtering criteria.
Use this query to preview a template that is not created yet (that is, apply a set of parameters to the template and view the result). Both the template values and the parameters are provided as input to the query.
Mandatory. Provides data about the template that is to be previewed.
params
String
Mandatory. Specifies the parameters to be applied. This must be a stringified JSON object, where each key is the parameter name, and the key’s value is the parameter value.
PreviewTemplateInput input
Attribute
Type
Description
type
TemplateType!
Mandatory. Specifies the kind of the template. Valid values: email, document.
subject
String
Optional, applicable only to email templates. Specifies the subject of the template.
body
String!
Mandatory. The body of the template that you would like to preview.
contentType
contentType!
Mandatory. Specifies if this is a plain text or HTML template. Valid values: plain, html.
css
String
Optional. Specifies any CSS rules defined locally at template level that override the external CSS (the so-called “parent” CSS).
parentCssId
ID
Optional. The ID of the external CSS. If provided, this must be a reference to a CSS object ID created previously.
Result
RenderingResult type
Attribute
Type
Description
subject
String
The rendered subject. Applicable only for email templates.
body
String
The rendered body.
contentType
ContentType
The template’s content type (HTML or plain text).
render
Use this query to render an existing template (that is, apply a set of parameters to the template and view the result). This query has the same outcome as the preview query, except that it accepts the template name and the template’s parameters as input.
Mandatory. The name of the template to be rendered.
params
String
Mandatory. Specifies the parameters to be applied. This must be a stringified JSON object, where each key is the parameter name, and the key’s value is the parameter value.
Mandatory. The name of the template to be created. Template names must be unique. If a template with this name already exists, an error message will be returned in the response.
type
TemplateType!
Mandatory. The type of the template to be created. Valid values: email, document.
subject
String
Optional. The subject of the template. Applicable only for email templates.
body
String!
Mandatory. The body of the template.
contentType
ContentType!
Mandatory. The content type of the template. Valid values: html, plain.
comment
String
Optional. Stores comment information pertaining to the template.