Unknown Field errors working with Email Templates in Management API

I’m working with the Management API and in particular right now working with the email templates. Anytime I try to do a Create or an Edit of an email template I’m getting an Unknown Field error. I’m likely doing something wrong as I’m just starting out here but I’m not seeing it.

Here is my code in C#:

public async Task<string> EditEmailTemplate(string? projectId, string? templateId)
{
    projectId = "project-test-XXXXXXXXXXXXXXXXXXXXX";
    templateId = "initial_style_template";

    var email_template_response = await _managementApiClient.Client.GetFromJsonAsync<GetEmailTemplateResponse>($"projects/{projectId}/email_templates/{templateId}");
    if (email_template_response == null)
    {
        return "Email template not found.";
    }

    email_template_response.EmailTemplate.Name += "x";

    // Serialize the new email template to JSON
    var content = new StringContent(JsonSerializer.Serialize(email_template_response.EmailTemplate), System.Text.Encoding.UTF8, "application/json");


    var resp = await _managementApiClient.Client.PutAsync($"projects/{projectId}/email_templates/{templateId}", content);
    var jsonString = await resp.Content.ReadAsStringAsync();
    email_template_response = JsonSerializer.Deserialize<GetEmailTemplateResponse>(jsonString);


    return "success";
}

My GetEmailTemplateResponse object is a simple class object of properties built from the specification defined on your website. It populates properly from the GET call so I’m getting the existing template just fine.

I then change the Name by adding an X on to it.

I then try to resend the template back up as a PUT.

If I look at JsonSerializer.Serialize(email_template_response.EmailTemplate) before it’s sent, it looks like this:

{
  "template_id": "initial_style_template",
  "name": "My first project style templatex",
  "sender_information": {
    "from_local_part": "",
    "from_domain": "",
    "from_name": "",
    "reply_to_local_part": "",
    "reply_to_name": ""
  },
  "prebuilt_customization": {
    "button_border_radius": 0,
    "button_color": "#106ee9",
    "button_text_color": "#ffffff",
    "font_family": "UNKNOWN_FONT_FAMILY",
    "text_alignment": "LEFT"
  },
  "custom_html_customization": null
}

What I get back is this:

{
  "status_code": 400,
  "error_message": "unknown field: \"template_id\" at line 1:2",
  "error_type": "invalid_argument",
  "request_id": "request-id-prod-48b7f052-4081-48fc-942d-c3d77b7e26d1"
}

Hi Wesley,

Thanks for providing your code snippet! The error message we’re seeing with this request_id is: Error binding request data to struct.

Looking at the body of your PUT request, it looks like you may need to wrap the object within a email_template object. Feel free to refer to our docs, here!

Let me know if that helps address the error you’re seeing. If not, we’re happy to dig further!

Thank you Justine!

That worked. It’s likely that’s standard for working directly with Rest APIs and it’s been so long since I’ve done that my brain pushed that detail out. Thanks again.

2 Likes

For sure, I’m glad that did the trick! Don’t hesitate to reach out if anything else comes up!