> For the complete documentation index, see [llms.txt](https://api-docs.everreal.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://api-docs.everreal.co/endpoints/tenants/tenant-mutation.md).

# Tenant Mutation

Create or Update in EverReal

### Mutation Types

{% hint style="info" %}
`upsertTenant` creates or updates a [tenant](https://api-docs.everreal.co/key-concepts.md#tenant). The **`externalId`** decides which one: a known `externalId` updates the tenant, and a new one creates it. To change the `externalId`, send the EverReal [uuid](https://api-docs.everreal.co/key-concepts.md#uuid) as `id`, together with the new `externalId`.
{% endhint %}

```graphql
type Mutation {
    upsertTenant(tenant: TenantInput): Tenant
    deleteTenant(externalId: String): Boolean
    removeTenant(id: String, shouldDeleteContracts: Boolean): AsyncEventResponse
}
```

What each mutation does:

* `upsertTenant` creates or updates a tenant. EverReal users cannot change a tenant that comes from your integration. Change it with this mutation.
* `deleteTenant` removes the link between the tenant and your integration. The tenant stays in EverReal, and EverReal users can then change it.
* `removeTenant` removes the tenant from EverReal. First remove all [contracts](https://api-docs.everreal.co/key-concepts.md#contract) of the tenant (see [Contract Mutation](https://api-docs.everreal.co/endpoints/contract/contract-mutation.md)). The mutation also takes `shouldDeleteContracts`.

### Schema Definition

{% hint style="warning" %}
Fields marked with **!** are required.
{% endhint %}

```graphql
input TenantInput {
  id:String #EverReal UUID typically can be passes in case of updating externalId
  externalTenantId: String! #externalTenantId by which the tenent is identified
  unitId: String # the unit of the tenant. Send unitId or externalUnitId
  externalUnitId: String # the unit of the tenant. Send unitId or externalUnitId
  firstName: String!
  lastName: String!
  email: String
  companyName: String
  phoneNumber: String
  cellPhoneNumber: String
  additionalTenants: [AdditionalTenant]
  coldRent: String       # number format should be numeric "123456.34" 
  serviceCharges: String # number format should be numeric "123456.34"
  heatingCosts: String   # number format should be numeric "123456.34"
  totalRent: String      # number format should be numeric "123456.34"
  securityDeposit: String # number format should be numeric "123456.34"
  contractStartDate: String # date format "DD.MM.YYYY". EverReal stores it as "YYYY-MM-DD"
  contractEndDate: String # date format "DD.MM.YYYY". EverReal stores it as "YYYY-MM-DD"
  terminationDate: String #date format should be "YYYY-MM-DD"
  meta: MetaInformation!
  customFieldValues: [CustomFieldValueInput]
}

input AdditionalTenant {
  externalId: String
  firstName: String
  lastName: String
  email: String
  phoneNumber: String
}

input MetaInformation {
  source: String!
}

input CustomFieldValueInput {
  key: String!
  value: JSON
}

type AsyncEventResponse {
  statusCode: Int
  message: String
}
```

Example: create or update a tenant. Only the fields marked with **!** are required.

{% tabs %}
{% tab title="graphQL" %}

```graphql
mutation {
  upsertTenant(
    tenant: {
      meta: { source: "INTEGRATION_SOURCE" }
      externalTenantId: "91001+006"
      externalUnitId: "11092+1101"
      firstName: "Erika"
      lastName: "Mustermann"
      email: "erika.mustermann@example.com"
      coldRent: "900.00"
      serviceCharges: "880.00"
      heatingCosts: "3.00"
      totalRent: "500"
      securityDeposit: "500"
      contractStartDate: "01.01.2020"
      contractEndDate: "01.01.2024" 
      companyName: "Example GmbH"
    }
  ) { id }
}
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer ....");
myHeaders.append("Content-Type", "application/json");

var graphql = JSON.stringify({
  query:
    '\r\n mutation {\r\n  upsertTenant(\r\n    tenant: {\r\n      meta: { source: "INTEGRATION_SOURCE" }\r\n      externalTenantId: "91001+0069330"\r\n      externalUnitId: "11092+1101122211s"\r\n      firstName: "Erika"\r\n      lastName: "Mustermann"\r\n      email: "erika.mustermann@example.com"\r\n      coldRent: "900.00"\r\n      serviceCharges: "880.00"\r\n      heatingCosts: "3.00"\r\n      totalRent: "500"\r\n      securityDeposit: "500"\r\n      contractStartDate: "01.01.2020"\r\n      contractEndDate: "01.01.2024"\r\n      placeOfBirth: "Berlin"\r\n      companyName: "Example GmbH"\r\n    }\r\n  ) {\r\n    id\r\n  }\r\n}',
  variables: {},
});
var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: graphql,
  redirect: 'follow'
};

fetch("https://{subdomain}.everreal.co/api/reporting/graphql", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

```

{% endtab %}
{% endtabs %}
