Welcome to mirror list, hosted at ThFree Co, Russian Federation.

api.md « developer-guide « docs - github.com/cydrobolt/polr.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a89e7d43922d32325f056df36f6970ffb4d7d1de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Polr API Documentation
-----------------------------

## API keys
To authenticate a user to Polr, you will need to provide an API key along with
each request to the Polr API, as a GET or POST parameter. (e.g `?key=API_KEY_HERE`)

## Assigning an API key
To assign an API key, log on from an administrator account, head over to the "Admin"
tab, and scroll to the desired user. From there, you can open the API button dropdown to
reset, create, or delete the user's API key. You will also be prompted to set a desired API quota. This is defined as requests per minute. You may allow unlimited requests by making the quota negative. Once the user receives an API key, they will be able to see an "API"
tab in their user panel, which provides the information necessary to interact with the API.

Alternative method: You can also assign a user an API key by editing their entry in the
`users` database table, editing the `api_key` value to the desired API key, `api_active` to the correct value (`1` for active, `0` for inactive), and `api_quota` to the desired API quota (see above).

## Actions
Actions are passed as a segment in the URL. There are currently
two actions implemented:

- `shorten` - shortens a URL
- `lookup` - looks up the destination of a shortened URL

Actions take arguments, which are passed as GET or POST parameters.
See [API endpoints](#api-endpoints) for more information on the actions.

## Response Type
The Polr API will reply in `plain_text` or `json`. The response type can be
set by providing the `response_type` argument to the request. If not provided,
the response type will default to `plain_text`.

Example `json` responses:
```
{
    "action": "shorten",
    "result": "https://example.com/5kq"
}
```

```
{
    "action":"lookup",
    "result": {
        "long_url": "https:\/\/google.com",
        "created_at": {
            "date":"2016-02-12 15:20:34.000000",
            "timezone_type":3,
            "timezone":"UTC"
        },
        "clicks":"0"
    }
}
```

Example `plain_text` responses:

```https://example.com/5kq```

```https://google.com```

## API Endpoints
All API calls will commence with the base URL, `/api/v2/`.

### /api/v2/action/shorten
Arguments:

 - `url`: the URL to shorten (e.g `https://google.com`)
 - `is_secret` (optional): whether the URL should be a secret URL or not. Defaults to `false`. (e.g `true` or `false`)
 - `custom_ending` (optional): a custom ending for the short URL. If left empty, no custom ending will be assigned.


Response: A JSON or plain text representation of the shortened URL.

Example: GET `http://example.com/api/v2/action/shorten?key=API_KEY_HERE&url=https://google.com&custom_ending=CUSTOM_ENDING&is_secret=false`
Response:
```
{
    "action": "shorten",
    "result": "https://example.com/5kq"
}
```

Remember that the `url` argument must be URL encoded.

### /api/v2/action/lookup
The `lookup` action takes a single argument: `url_ending`. This is the URL to
lookup. If it exists, the API will return with the destination of that URL. If
it does not exist, the API will return with the status code 404 (Not Found).

Arguments:

 - `url_ending`: the link ending for the URL to look up. (e.g `5ga`)
 - `url_key` (optional): optional URL ending key for lookups against secret URLs

Remember that the `url` argument must be URL encoded.

Example: GET `http://example.com/api/v2/action/lookup?key=API_KEY_HERE&ending=2`
Response:
```
{
    "action": "lookup",
    "result": "https://google.com"
}
```

## HTTP Error Codes
The API will return an error code if your request was malformed or another error occured while processing your request.

### HTTP 400 Bad Request
This status code is returned in the following circumstances:

- By the `shorten` endpoint
    - In the event that the custom ending provided is already in use, a `400` error code will be returned and the message `custom ending already in use` will be returned as an error.
- By any endpoint
    - Your request will return a `400` if it is malformed or the contents of your arguments do not fit the required data type.

### HTTP 500 Internal Server Error

- By any endpoint
    - The server has encountered an unhandled error. This is most likely due to a problem with your configuration or your server is unable to handle the request due to a bug.

### HTTP 401 Unauthorized

 - By any endpoint
    - You are unauthorized to make the transaction. This is most likely due to an API token mismatch, or your API token has not be set to active.
 - By the `lookup` endpoint
    - You have not provided the valid `url_key` for a secret URL lookup.


### HTTP 404 Not Found

- By the `lookup` endpoint

    - Returned in the circumstance that the short URL to look up was not found in the database.

### HTTP 403 Forbidden

- By the `shorten` endpoint
    -  Your request was understood, but you have exceeded your quota.

## Error Responses
Example `json` error response:
```
{
    "error": "custom ending already in use"
}
```

Example `plain_text` error response:

`custom ending already in use`

## Testing the API

You may test your integrations on http://demo.polr.me with the credentials "demo-admin"/"demo-admin". Keep in mind the instance is only a demo and may be cleared at any time.