Formatting
Templates are powerful. Use them wisely to transform your webhook data exactly as needed.
Introduction
Webhooked uses Go templates enhanced with 100+ functions from go-sprout to transform webhook data. This powerful templating system allows you to reshape, validate, and enrich data before storage or in responses.
You can validate your template by using the command webhooked --validate --config webhooked.yaml
Template Functions
Webhooked use internally go-template for functionality and sprout to provide 100+ functions.
Full documentation of sprout are available here : Sprout
Template String
For simple template or a desire to have all configuration inside the yaml file, you can put your tempalte directly inline
formatting:
templateString: |
{{- with $data := fromJSON .Payload -}}
{{- $timestamp := $data.created_at | toDate "2006-01-02T15:04:05Z07:00" | unixEpoch -}}
{
"id": "{{ $data.id }}",
"created": "{{ $timestamp }}",
"reference": "{{ $data.id }}-{{ $timestamp }}"
}
{{- end -}}Template Files
For complex templates, usage of external files are recommended
Template file (webhook-transform.tmpl):
Best Practices
Always parse JSON once and reuse the result
Use
withblocks for nil-safetyProvide defaults for optional fields
Use external files for complex templates
Cache computed values in variables
Document complex logic with comments
Test templates with various payloads
Performance Optimization
Parse Once, use multiple times
✅ GOOD: Parse one
❌ BAD: Parse multiple times
Avoid Unnecessary Operations
✅ GOOD: Cache computed values
❌ BAD: Recall functions multiples times can cause bug and latency
Avoid String building
✅ GOOD: Use template engine
❌ BAD: use go functions to build end strings
Common Issues and Solutions
Issue: Template Syntax Errors
Issue: JSON Escaping
Issue: Nil Pointer
Last updated