> ## Documentation Index
> Fetch the complete documentation index at: https://docs.auxia.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Insert/Update Treatment Type

> APIs for inserting and updating treatment types via API.

This document provides a guide and overview for the InsertTreatmentType and UpdateTreatmentType RPCs, which allow you to insert and update treatment types directly via API.

## API Definitions

```url theme={null}
POST https://apis.auxia.io/v1/InsertTreatmentType
```

```url theme={null}
POST https://apis.auxia.io/v1/UpdateTreatmentType
```

## Entities

### SurfacePb

```
message SurfacePb {
  string surface_name = 1;
}
```

<Note>
  Every Surface named here must already exist for the project, otherwise the request is rejected with `400`. Use [Insert Surface](/api-reference/treatment-management/insert-surface) to register a Surface first.
</Note>

### TreatmentContentFieldTypePb

```
enum ContentFieldDataType {
  CONTENT_FIELD_DATA_TYPE_UNSPECIFIED = 0;
  STRING = 1;
  HTML = 2;
}
message TreatmentContentFieldTypePb {
  // Required
  string field_name = 1;

  // Required
  // Maximum Length allowed for the field
  int32 maximum_length_allowed = 2;

  // Optional
  // Set it to true if multiple language support is required.
  // By default it will be set to false.
  bool is_per_language = 3;

  // Required
  // By default it will be set to STRING
  ContentFieldDataType data_type = 4;
}
```

### TreatmentTypePb

```
message TreatmentTypePb {
int64 treatment_type_id = 1;
string project_id = 2;
string treatment_type_name = 3;
repeated TreatmentContentFieldTypePb content_field_types = 4;
repeated SurfacePb surfaces = 5;
}
```

## InsertTreatmentType

### API to insert Treatment Type

```
message InsertTreatmentTypeRequest {
  // Required
  string project_id = 1;

  // Required
  string treatment_type_name = 2;

  // Optional. May be empty: a treatment type can exist with zero surfaces (e.g. while it is being
  // migrated between surfaces). When present, surface names must be unique and must exist.
  repeated SurfacePb surfaces = 3;

  // Required
  repeated TreatmentContentFieldTypePb content_field_types = 4;

  // Required
  string last_modified_by = 5;
}
message InsertTreatmentTypeResponse {
  TreatmentTypePb treatment_type = 1;
}

```

### Sample cURL Request

```
curl --location --request POST 'https://apis.auxia.io/v1/InsertTreatmentType' \
--header 'Content-Type: application/json' \
--header 'x-api-key: TEST_API_KEY' \
--data-raw '{
  "projectId": "PROJECT_ID",
  "treatmentTypeName": "TREATMENT_TYPE",
  "lastModifiedBy": "USER_EMAIL",
  "surfaces": [
    {
      "surfaceName": "IN_APP_CONTENT"
    }
  ],
  "contentFieldTypes": [
     {
       "fieldName": "title",
       "maximumLengthAllowed": 20
     },
     {
       "fieldName": "body",
       "maximumLengthAllowed": 100
     }   
  ]
}'
```

### Sample Response

```
{
  "treatmentType": {
    "treatmentTypeId": "TREATMENT_TYPE_ID",
    "projectId": "PROJECT_ID",
    "treatmentTypeName": "TREATMENT_TYPE",
    "contentFieldTypes": [
      {
        "fieldName": "title",
        "maximumLengthAllowed": 20,
        "dataType": "STRING"
      },
      {
        "fieldName": "body",
        "maximumLengthAllowed": 100,
        "dataType": "STRING"
      }
    ],
    "surfaces": [
      {
        "surfaceName": "IN_APP_CONTENT"
      }
    ]
  }
 }
```

## UpdateTreatmentType

### API to update Treatment Type

```
message UpdateTreatmentTypeRequest {
  // Required
  string project_id = 1;

  // Required
  int64 treatment_type_id = 2;

  // Optional
  // If the field is not set, treatment_type_name will not be changed.
  // Empty treatment_type_name is not allowed.
  optional string treatment_type_name = 3;

  // Optional
  // If the field is not set, surfaces will not be changed. An empty list on its own also means
  // "not changed", because a repeated field cannot distinguish unset from empty. To detach every
  // surface, send an empty list together with clear_surfaces = true.
  repeated SurfacePb surfaces = 4;

  // Required
  repeated TreatmentContentFieldTypePb content_field_types = 5;

  // Required
  string last_modified_by = 6;

  // Optional
  // Set to true, with surfaces empty, to detach every surface from the treatment type. A treatment
  // type with zero surfaces is a valid state. Setting this to true with a non-empty surfaces list is
  // rejected.
  optional bool clear_surfaces = 7;
}
message UpdateTreatmentTypeResponse {
  TreatmentTypePb treatment_type = 1;
}

```

### Sample cURL Request

```
curl --location --request POST 'https://apis.auxia.io/v1/UpdateTreatmentType' \
--header 'Content-Type: application/json' \
--header 'x-api-key: TEST_API_KEY' \
--data-raw '{
  "projectId": "PROJECT_ID",
  "treatmentTypeName": "TREATMENT_TO_BE_INSERTED",
  "treatmentTypeId": "TREATMENT_TYPE_ID",
  "lastModifiedBy": "USER_EMAIL",
  "surfaces": [
    {
      "surfaceName": "IN_APP_CONTENT"
    }
  ],
  "contentFieldTypes": [
     {
       "fieldName": "title",
       "maximumLengthAllowed": 20
     },
     {
       "fieldName": "body",
       "maximumLengthAllowed": 100
     },
     {
       "fieldName": "cta_url",
       "maximumLengthAllowed": 50
     }  
  ]
}'
```

### Sample cURL Request: detaching every surface

Omit `surfaces` and set `clearSurfaces` to `true`. Omitting `surfaces` without `clearSurfaces` leaves the existing surfaces in place.

```
curl --location --request POST 'https://apis.auxia.io/v1/UpdateTreatmentType' \
--header 'Content-Type: application/json' \
--header 'x-api-key: TEST_API_KEY' \
--data-raw '{
  "projectId": "PROJECT_ID",
  "treatmentTypeId": "TREATMENT_TYPE_ID",
  "lastModifiedBy": "USER_EMAIL",
  "clearSurfaces": true,
  "contentFieldTypes": [
     {
       "fieldName": "title",
       "maximumLengthAllowed": 20
     }
  ]
}'
```

### Sample Response

```
{
  "treatmentType": {
    "treatmentTypeId": "TREATMENT_TYPE_ID",
    "projectId": "PROJECT_ID",
    "treatmentTypeName": "TREATMENT_TO_BE_INSERTED",
    "contentFieldTypes": [
      {
        "fieldName":"title",
        "maximumLengthAllowed":20,
        "dataType":"STRING"
      },
      {
        "fieldName":"body",
        "maximumLengthAllowed":100,
        "dataType":"STRING"
      },
      {
        "fieldName": "cta_url",
        "maximumLengthAllowed": 50,
        "dataType": "STRING"
      }
    ],
    "surfaces": [
      {
        "surfaceName": "IN_APP_CONTENT"
      }
    ]
  }
 }
```
