|
| 1 | +--- |
| 2 | +title: Calling the API with Javascript |
| 3 | +navigation: jQuery Demo |
| 4 | +description: A jQuery example accessing the Kimai API |
| 5 | +related: |
| 6 | + - rest-api |
| 7 | + - rest-api-examples |
| 8 | +--- |
| 9 | + |
| 10 | +If you develop your own [plugin]({% link _documentation/developer/plugins.md %}) and need to use the API for logged-in |
| 11 | +user, |
| 12 | +then you don't have to care about authentication, Kimai will handle it for you. |
| 13 | + |
| 14 | +Copy & paste this code into a new `api.html` file and open it in your browser. |
| 15 | +You can execute some sample requests and see the JSON result. |
| 16 | + |
| 17 | +```html |
| 18 | +<!doctype html> |
| 19 | +<html lang="en"> |
| 20 | +<head> |
| 21 | + <meta charset="utf-8"> |
| 22 | + <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> |
| 23 | + <title>Kimai - API demo</title> |
| 24 | + <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> |
| 25 | + <link rel="stylesheet" href="https://getbootstrap.com/docs/4.3/examples/floating-labels/floating-labels.css"> |
| 26 | + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/themes/prism.min.css"> |
| 27 | + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/plugins/line-numbers/prism-line-numbers.min.css"> |
| 28 | + <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> |
| 29 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script> |
| 30 | + <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script> |
| 31 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/prism.min.js"></script> |
| 32 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/components/prism-json.min.js"></script> |
| 33 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/plugins/line-numbers/prism-line-numbers.min.js"></script> |
| 34 | + <style> |
| 35 | + body { |
| 36 | + display: block; |
| 37 | + } |
| 38 | +
|
| 39 | + .codePreview { |
| 40 | + margin-top: 30px; |
| 41 | + } |
| 42 | + </style> |
| 43 | + <script> |
| 44 | + function callKimaiApi(method, successHandler, errorHandler) { |
| 45 | + var domain = $('#inputDomain').val(); |
| 46 | + var token = $('#inputToken').val(); |
| 47 | + $.ajax({ |
| 48 | + url: domain + '/api/' + method, |
| 49 | + type: 'GET', |
| 50 | + beforeSend: function (request) { |
| 51 | + request.setRequestHeader('Authorization', 'Bearer ' + token); |
| 52 | + }, |
| 53 | + headers: { |
| 54 | + 'Authorization': 'Bearer ' + token, |
| 55 | + }, |
| 56 | + success: successHandler, |
| 57 | + error: errorHandler |
| 58 | + }); |
| 59 | + } |
| 60 | +
|
| 61 | + $(function () { |
| 62 | + $('#loginForm').on('submit', function (event) { |
| 63 | + event.preventDefault(); |
| 64 | + event.stopPropagation(); |
| 65 | + $('#loginButton').text('Loading...'); |
| 66 | + callKimaiApi('version', function (result) { |
| 67 | + $('#loginButton').text('Success'); |
| 68 | + $('.secret').attr('style', 'display:block'); |
| 69 | + return false; |
| 70 | + }, function (xhr, err) { |
| 71 | + $('#loginButton').text('Try again!'); |
| 72 | + $('.secret').attr('style', 'display:none'); |
| 73 | + console.log(xhr); |
| 74 | + alert('Error occured, see console for details'); |
| 75 | + } |
| 76 | + ); |
| 77 | + return false; |
| 78 | + }); |
| 79 | +
|
| 80 | + $('button[data-api]').on('click', function (event) { |
| 81 | + event.preventDefault(); |
| 82 | + event.stopPropagation(); |
| 83 | +
|
| 84 | + var apiMethod = $(this).attr('data-api'); |
| 85 | + var breakAttr = $(this).attr('data-attribute-break'); |
| 86 | + $('#loginButton').text('Loading...'); |
| 87 | +
|
| 88 | + callKimaiApi( |
| 89 | + apiMethod, |
| 90 | + function (result) { |
| 91 | + $('#loginButton').text('Success!'); |
| 92 | + var jsonBeauty = JSON.stringify(result).trim(); |
| 93 | + if (breakAttr === "true") { |
| 94 | + jsonBeauty = jsonBeauty.split('","').join('",' + "\n" + '"'); |
| 95 | + } |
| 96 | + jsonBeauty = jsonBeauty.split('},{').join('},' + "\n" + '{'); |
| 97 | + $('#apiResult').text(jsonBeauty); |
| 98 | + $('.codePreview').attr('style', 'display:block'); |
| 99 | + Prism.highlightElement(document.getElementById('apiResult')); |
| 100 | + return false; |
| 101 | + }, function (xhr, err) { |
| 102 | + $('#loginButton').text('Sorry, that failed :-('); |
| 103 | + console.log(xhr); |
| 104 | + alert('Error occured, see console for details'); |
| 105 | + } |
| 106 | + ); |
| 107 | + return false; |
| 108 | + }); |
| 109 | + }); |
| 110 | + </script> |
| 111 | +</head> |
| 112 | +<body> |
| 113 | +<div class="container"> |
| 114 | + <form id="loginForm" class="form-signin"> |
| 115 | + <div class="text-center mb-4"> |
| 116 | + <h1 class="h3 mb-3 font-weight-normal">Kimai – API Demo</h1> |
| 117 | + <p>Please provide your API URL and token below</p> |
| 118 | + </div> |
| 119 | + <div class="form-label-group"> |
| 120 | + <input type="url" id="inputDomain" class="form-control" placeholder="https://www.example.com/" required |
| 121 | + autofocus value="https://demo.kimai.org"> |
| 122 | + <label for="inputDomain">Kimai base URL (domain + port)</label> |
| 123 | + </div> |
| 124 | + <div class="form-label-group"> |
| 125 | + <input type="text" id="inputToken" class="form-control" placeholder="API Token" required |
| 126 | + value="api_kitten_super"> |
| 127 | + <label for="inputToken">API Token</label> |
| 128 | + </div> |
| 129 | + <button class="btn btn-lg btn-primary btn-block" id="loginButton" type="submit">Sign in</button> |
| 130 | + </form> |
| 131 | + <div class="row secret" style="display:none"> |
| 132 | + <div class="col-sm text-center"> |
| 133 | + <button type="button" class="btn btn-primary" data-api="ping" data-attribute-break="true">Ping</button> |
| 134 | + <button type="button" class="btn btn-secondary" data-api="version" data-attribute-break="true">Version</button> |
| 135 | + <button type="button" class="btn btn-secondary" data-api="plugins" data-attribute-break="true">Plugins</button> |
| 136 | + <button type="button" class="btn btn-primary" data-api="timesheets" data-attribute-break="false">Timesheet</button> |
| 137 | + <button type="button" class="btn btn-primary" data-api="activities" data-attribute-break="false">Activities</button> |
| 138 | + <button type="button" class="btn btn-primary" data-api="projects" data-attribute-break="false">Projects</button> |
| 139 | + <button type="button" class="btn btn-primary" data-api="customers" data-attribute-break="false">Customers</button> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + <div class="row codePreview" style="display:none"> |
| 143 | + <pre class="language-json line-numbers" style="white-space: pre-line"> |
| 144 | + <code id="apiResult"></code> |
| 145 | + </pre> |
| 146 | + </div> |
| 147 | +</div> |
| 148 | +</body> |
| 149 | +</html> |
| 150 | +``` |
| 151 | + |
0 commit comments