# Set up team booking

Thanks to the API, you are able to group your user by team. The team API is really versatile so that you can represent all types of organizational structure, from hierarchical to matrix structure.

Here we will learn how to create and manage our first team. At the end, we will see how to book a team event.

NB: a team event isn't an event between the members of a team. A team event allows you to book an appointment with a member of the team. For instance, you can create an event with the Sales team, and it will automatically assign the event to someone of the sales team (based on some parameters you can set up).

# Create your first team

Let's create our first team. A team is composed of admins and members. But don't forget that the admins are not member of the team by default. If you want an admin to be part of the team, you have to add him as a member too.

So, for this guide, we will create a Sales team with 1 admin (who is also member of the team) and an other member. Note that all the ids are fake and are here only for the purpose of the example.

To create our first team, let's just make a POST request at /v2/teams :

curl --request POST 'https://api.vyte.in/v2/teams' \
--header 'Authorization: 2lnpjjrurrl49xja5oo0qujtl60embr7zppiphc5fcav4n7ycx' \
--header 'Content-Type: application/json' \
--data-raw '{
  "admins": [
    "5eecc40bb2181073ac6ff375"
  ],
  "members": [
    "5eecc40bb2181073ac6ff375",
    "5f198d23c1ac5d0bcafc00ee"
  ],
  "extid": "1",
  "name": "sales",
  "level_name": "company"
}'

Our first team is now created 👏

{
  "admins": ["5eecc40bb2181073ac6ff375"],
  "members": ["5eecc40bb2181073ac6ff375", "5f198d23c1ac5d0bcafc00ee"],
  "_id": "5f47af4d2285550793c632c4",
  "organization": "5f198da1c1ac5d1a30fc00f3",
  "extid": "1",
  "name": "sales",
  "level_name": "department",
  "updatedAt": "2020-08-27T13:06:28.430Z",
  "createdAt": "2020-08-27T13:04:13.588Z",
  "__v": 2
}

# Manage your team

Oh! Someone new was just hired in the sales team. We have to add him to the sales team.

As there is no dedicated endpoint to add or remove members for a team, we will use the update team's endpoint and modify the members array.

Let's make a PUT request at /v2/team/5f47af4d2285550793c632c4 :

curl --location --request PUT 'https://api.vyte.in/v2/teams/5f47af4d2285550793c632c4' \
--header 'Authorization: 2lnpjjrurrl49xja5oo0qujtl60embr7zppiphc5fcav4n7ycx' \
--header 'Content-Type: application/json' \
--data-raw '{
  "members": [
      "5eecc40bb2181073ac6ff375",
      "5f198d23c1ac5d0bcafc00ee",
      "5f2809b0f678a7a0227f94ac"
  ]
}'

And now our new Sales is part of the team.

# Get available slots for your team

To get all the slots when at least one member of your team is available, you can use the /v2/slots or /v2/slots/days endpoints as such:

curl --request GET 'https://api.vyte.in/v2/slots?duration=30&team=5f47af4d2285550793c632c4&from=2020-09-01&to=2020-09-02' \
--header 'Authorization: 2lnpjjrurrl49xja5oo0qujtl60embr7zppiphc5fcav4n7ycx'

We get in response a list of slots so you can let your user pick one.

{
  "timezone": "Europe/Paris",
  "from": "2020-09-01",
  "to": "2020-09-02",
  "slots": [
    {
      "start": {
        "dateTime": "2020-09-01T02:00:00+02:00"
      },
      "end": {
        "dateTime": "2020-09-01T02:30:00+02:00"
      }
    },
    {
      "start": {
        "dateTime": "2020-09-01T02:30:00+02:00"
      },
      "end": {
        "dateTime": "2020-09-01T03:00:00+02:00"
      }
    },
    {
      "start": {
        "dateTime": "2020-09-01T03:00:00+02:00"
      },
      "end": {
        "dateTime": "2020-09-01T03:30:00+02:00"
      }
    },
    {
      "start": {
        "dateTime": "2020-09-01T03:30:00+02:00"
      },
      "end": {
        "dateTime": "2020-09-01T04:00:00+02:00"
      }
    },
    {
      "start": {
        "dateTime": "2020-09-01T04:00:00+02:00"
      },
      "end": {
        "dateTime": "2020-09-01T04:30:00+02:00"
      }
    },
    {
      "start": {
        "dateTime": "2020-09-01T04:30:00+02:00"
      },
      "end": {
        "dateTime": "2020-09-01T05:00:00+02:00"
      }
    },
    {
      "start": {
        "dateTime": "2020-09-01T09:00:00+02:00"
      },
      "end": {
        "dateTime": "2020-09-01T09:30:00+02:00"
      }
    },
    ...
  ]
}

# Create your first team event

As we explained before, the Team API allows you to create events and automatically assign the event to someone of the team who is available on the requested date.

It works quite the same as a classic event creation. The only things to change is that you can't send several dates and you don't have to provide creator information (because he will be automatically chosen by the API).

So, we'are ready to create our first team event :

curl --location --request POST 'https://api.vyte.in/v2/teams/5f47af4d2285550793c632c4/events' \
--header 'Authorization: 2lnpjjrurrl49xja5oo0qujtl60embr7zppiphc5fcav4n7ycx' \
--header 'Content-Type: application/json' \
--data-raw '{
  "title": "First created event",
  "dates": [
    {
      "date": "2020-09-01T09:00:00",
      "end_date": "2020-09-01T10:00:00"
    }
  ],
  "places": [
    {
      "name": "Place for the meeting."
    }
  ],
  "timezone": "Europe/Paris",
  "invitees": [{
    "email": "john.doe@personmakingthebooking.com",
    "full_name": "John Doe"
  }]
}'

We get in response the event created and we can see that the creator was automatically chosen:

{
  "created_by": {
    "user": "5f3feb7821046c3bb9327e6a",
    "full_name": "John Doe",
    "email": "john.doe@example.com"
  },
  "confirmed": {
    "flag": false,
    "updated_at": null,
    "multi": false
  },
  "third_party": {
    "group_ids": []
  },
  "ics_sequence": 0,
  "version": 0,
  "group_pro": false,
  "identification_alternatives": [],
  "_id": "5f47b6c4228555e2d6c63311",
  "title": "Second created event",
  "dates": [
    {
      "created_by": {
        "user": "5f3feb7821046c3bb9327e6a"
      },
      "votes": {
        "yes": [
          {
            "created_by": {
              "user": "5f3feb7821046c3bb9327e6a"
            },
            "_id": "5f47b6c4228555e269c63315"
          }
        ],
        "no": []
      },
      "all_day": false,
      "confirmed": false,
      "confirmed_invitees": [],
      "_id": "5f47b6c42285550d40c63312",
      "date": "2020-09-16T07:00:00.000Z",
      "end_date": "2020-09-16T08:00:00.000Z",
      "updatedAt": "2020-08-27T13:36:04.246Z",
      "createdAt": "2020-08-27T13:36:04.246Z"
    }
  ],
  "places": [
    {
      "created_by": {
        "user": "5f3feb7821046c3bb9327e6a"
      },
      "votes": {
        "yes": [
          {
            "created_by": {
              "user": "5f3feb7821046c3bb9327e6a"
            },
            "_id": "5f47b6c422855573b8c63316"
          }
        ],
        "no": []
      },
      "source": "app",
      "_id": "5f47b6c42285554c63c63313",
      "name": "Place for the meeting.",
      "updatedAt": "2020-08-27T13:36:04.246Z",
      "createdAt": "2020-08-27T13:36:04.246Z"
    }
  ],
  "timezone": "Europe/Paris",
  "invitees": [
    {
      "created_by": {
        "user": "5f3feb7821046c3bb9327e6a"
      },
      "stats": {
        "voted": {
          "dates": true,
          "places": true
        }
      },
      "star": false,
      "_id": "5f47b6c42285551c4fc63314",
      "user": "5f3feb7821046c3bb9327e6a",
      "email": "john.doe@example.com",
      "su": true,
      "full_name": "John Doe",
      "timezone": "Europe/London",
      "locale": "en"
    }
  ],
  "messages": [],
  "declines": [],
  "lang": "en",
  "locale": "en",
  "updatedAt": "2020-08-27T13:36:04.246Z",
  "createdAt": "2020-08-27T13:36:04.246Z",
  "invitees_length": 1,
  "email": "reply-to-participants-to-second-created-event-dvumxzapx@vytein.mailgun.org",
  "key_store": {
    "_id": "5f47b6c42285554f44c63317",
    "hash": {
      "5f3feb7821046c3bb9327e6a": "msg0ls0oxjvtjtg3"
    },
    "updatedAt": "2020-08-27T13:36:04.252Z",
    "createdAt": "2020-08-27T13:36:04.252Z",
    "__v": 0
  },
  "links": null,
  "__v": 0
}

And that's how you can use the API to automate appointment requests with teams.