# Key Availability

**GET** `/v1/keys/{key}`

Returns public information on an API Key: whether it can be used right now (`available`), the search contexts the key is licensed for (`contexts`) and the context that best matches the caller's IP address (`context`).

The endpoint accepts API Keys (beginning `ak_`) and sub-licensed keys (beginning `sl_`), and needs no Management Key.

A key that exists but cannot be used, because it has no lookups left or has breached a limit, returns `200` with `"available": false`. An unknown or malformed key returns an error.

Supply a valid Management Key and the endpoint returns the key's private details instead, as `GET /keys/{key}/details` does. A Management Key that does not own the key is rejected.

## Example request

**URL**

```http
https://api.ideal-postcodes.co.uk/v1/keys/ak_test
```

**curl**

```bash
curl 'https://api.ideal-postcodes.co.uk/v1/keys/ak_test'
```

**JavaScript**

```javascript
import { createIdpcClient, keyAvailability } from '@ideal-postcodes/sdk';

const client = createIdpcClient({ apiKey: 'ak_test' });

const { data } = await keyAvailability({
  client,
  path: { key: 'ak_test' },
});
```

**Python**

```python
# pip install ideal-postcodes
from ideal_postcodes import IdealPostcodes

with IdealPostcodes("ak_test") as client:
    result = client.key_availability("ak_test")
```

**Ruby**

```ruby
require "net/http"
require "json"

uri = URI("https://api.ideal-postcodes.co.uk/v1/keys/ak_test")
result = JSON.parse(Net::HTTP.get(uri))["result"]
```

**PHP**

```php
<?php
// composer require ideal-postcodes/sdk
use IdealPostcodes\IdealPostcodes;

$client = IdealPostcodes::client('ak_test');
$result = $client->keyAvailability('ak_test')->result;
```

## Example response

**200 OK**

```json
{
  "result": {
    "contexts": [
      {
        "iso_3": "USA",
        "iso_2": "US",
        "description": "United States",
        "emoji": "🇺🇸",
        "rgeo": true
      }
    ],
    "context": "",
    "available": true
  },
  "message": "Success",
  "code": 2000
}
```

**404 Not Found**

response.jsonjson

```json
{
  "code": 4040,
  "message": "Invalid Key"
}
```

## Path parameters

* `key` string

  **API Key**

  The API Key. Begins `ak_`.

  Example: `ak_test`

## Response

`result` is a small, fixed object, shown here in full. Each address record in `contexts` describes a country the key may search within.

* `result` ApiKey

  * `contexts` object\[]

    A list of available contexts for a key

    | Field         | Type    | Description                                                            |
    | ------------- | ------- | ---------------------------------------------------------------------- |
    | `iso_3`       | string  | 3 letter ISO code. Example: `USA`                                      |
    | `iso_2`       | string  | 2 letter ISO code. Example: `US`                                       |
    | `description` | string  | Country descriptor to show in Address Finder. Example: `United States` |
    | `emoji`       | string  | Emoji text icon. Example: `🇺🇸`                                         |
    | `rgeo`        | boolean | Indicates availability of reverse geolocation search. Example: `true`  |

  * `context` one of

    Returns current context if it is in the list of available contexts for this key.

    * string

      Limits search results within a geographical boundary or country.

    * string

      Empty string if no context is provided or key check has failed

      Values: ""

  * `available` boolean

    Determines whether the key can be used by the requesting agent.

    Returns false if one of the following conditions is met:

    * Key has no lookups remaining
    * Daily limit has been reached on the key
    * Daily individual limit has been reached
    * Key is not being used via an authorised URL
    * (Sublicensed key only) Key has no valid licensee attached
    * (Sublicensed key only) Key is not being used via an authorised URL specified by licensee

    Example: `true`

The `result` sits inside the standard `{ result, code, message }` envelope, see the [API reference](/docs/api/api-reference.md) for the wrapper format.
