# Tenant Mutation

### Mutation Types

{% hint style="info" %}
Mutations are responsible to insert or update a specific register, the operation insert or update is defined by **`externalId`**, in case this tenant was imported previously, this mutation will updated the resource, otherwise will create the tenant. If you wish to update the externalId, pass EverReal UUID as id in the mutation payload along with the changed externalId
{% endhint %}

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

Here are details on the capabilities of different mutations

* The `upsertTenant`  mutation is used to create or update a tenant in Everreal system and tenant added to the system cannot be modified by Everreal and if needs to be modified it should be done via the same endpoint itself.
* The `deleteTenant`  mutation is used to delete the tenant relation with the external integration source, doing this will not delete the tenant but instead it will remain as a detached tenant from integration and can be modified using Everreal.
* The `removeTenant` mutation is used to remove the tenant from the system, in order to perform removeTenant, please make sure that all the contracts assosiated with the tenant is been removed ( [reference](/endpoints/contract/contract-mutation.md) to delete contract)

### Schema Definition

{% hint style="warning" %}
&#x20;items with **!** notation are required
{% endhint %}

<pre class="language-graphql"><code class="lang-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! #unitId by which the tenent will be attached to. You have to either pass unitId or exteranalunitId 
  externalUnitId: String! #exteranalunitId by which the tenent will be attached to. You have to either pass unitId or exteranalunitId
  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 should be "YYYY-MM-DD"
  contractEndDate: String #date format should be "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
}

<strong>type AsyncEventResponse {
</strong>  statusCode: Int
  message: String
}

</code></pre>

Below we are providing a full example how to create or update a tenant, all this information is not required, only the ones that was using **!** notation previously.

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

```graphql
mutation {
  upsertTenant(
    tenant: {
      meta: { source: "INTEGRATION_SOURCE" }
      externalTenantId: "91001+006"
      externalUnitId: "11092+1101"
      firstName: "Ivana"
      lastName: "Maric"
      email: "ivanamaric@everreal.co"
      coldRent: "900.00"
      serviceCharges: "880.00"
      heatingCosts: "3.00"
      totalRent: "500"
      securityDeposit: "500"
      contractStartDate: "01.01.2020"
      contractEndDate: "01.01.2024" 
      companyName: "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: "Ivana"\r\n      lastName: "Maric"\r\n      email: "ivana@masssric.de"\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: "thotta"\r\n      companyName: "Everreal Gmbh"\r\n    }\r\n  ) {\r\n    id\r\n  }\r\n}',
  variables: {},
});
var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: graphql,
  redirect: 'follow'
};

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

```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://api-docs.everreal.co/endpoints/tenants/tenant-mutation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
