# Handling responses

## Making Requests

All API calls require the `Medable-Client-Key` header. An Organization creates one or more applications, providing the public key to all its clients. Note that some web applications may employ the `Medable-Csrf-Token`. If an application is configured as such, each *authenticated* request must include HTTP response header of the same name, sent with the login response (and each subsequent authenticated response).

{% hint style="info" %}
For semantic reasons, API access to Objects is always through the use of the object plural name (e.g. /accounts).
{% endhint %}

### Example: GET/accounts/me

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

```javascript
$.ajax({
    url: "https://api.dev.medable.com/example/accounts/me",
    type: "GET",
    contentType: "application/json; charset=UTF-8",
    dataType : "json",
    headers: {
        "Medable-Client-Key": "GsAqlhnIMzrDeD8V2MBQWq",
        "Medable-Csrf-Token": "MvFkpcS0BCM9qpf4K0pFMyiE57e3VtI5Cb38NfQn6eWiPPp6CqZCOOrAljDZYWnU"
    }
});
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
[[MDAPIClient sharedClient]
 currentAccount:^(MDAccount* account, MDFault* fault)
 {
     if (fault)
     {
        // Fault handling
     }
     else
     {

     }
 }];
```

{% endtab %}
{% endtabs %}

## Handling Responses

API responses always consist of JSON documents with an object property, whose values will either list or the name of the object type, such as `account` or `connection`.

### Example: Single Object Response

### GET/account

```javascript
{
    "_id": "5516ee2634d8d93428169c0e",                       
    "object": "account",
    "roles": [
        "000000000000000000000007"
    ],
    ...    
}
```

When the response is an array, the `data` property contains the resulting array, and the `hasMore` property contains a boolean value that is true if there are more results that can be retrieved using Paging.

### Example: Multiple Object Response

### GET /c\_examples?limit=1

```javascript
{
    "data": [
        {
            "_id": "5519c01aae2fd2b02915c81f",                        
            "object": "c_example",
            "creator": {
                "_id": "5516ee2634d8d93428169c0e",
                "object": "Account",
                "path": "/accounts/5516ee2634d8d93428169c0e"
            },
            ...
        }
    ],
    "hasMore": true,
    "object": "list"
}
```

{% hint style="info" %}
Lists are typically sorted by `_id`, in descending order. As such, results will have the newest items at the front of the result set.
{% endhint %}

If the response is a fault, the *object* property will be *fault* (see Faults).

### GET /accounts/5516ee2634d8d93428169c0e/profile/missing

```javascript
{
    "object": "fault",
    "name": "error",
    "code": "kNotFound",
    "status": 404,
    "reason": "Property not found.",
    "message":"Resource not found.",
    "path":"profile.missing"
}
```

Some responses, such as those retrieved via direct Property Access, will have `result` as their object property, as they do not represent a concrete, registered object type. Note, however, that directly accessed array properties will always be `list` objects.

### Result Example

```javascript
GET /accounts/5516ee2634d8d93428169c0e/name

{
    "object": "result",
    "data": {
        "first": "John",
        "last": "Smith"
    }
}
```

### List Example

```javascript
{
    "object": "list",
    "data": [
        "000000000000000000000007"
    ],
    "hasMore": false
}
```
