openapi: 3.0.0
info:
title: UCL Analytics OAuth API
description: "An API generated by a team of student developers to interact with student and university data at UCL.\n\nThe API is made up of several services, each of which will be separately explained. Every service will be documented here with important information, tips and examples.\n\n## Get Your API Key\n\nBefore you can use the API you should head to the API Dashboard and sign up using your UCL user account. Once logged into the dashboard simply name your app and you’ll be given a key that you can use to access all the services. Simple!\n\n## API Rate Limits\n\nRate limiting of the API is primarily on a per-user basis. The limit is calculated against the user, across all their access tokens.\n\nThe limit is 10,000 requests per day and resets every day at midnight, London time.\n\nWhen a request is throttled, the response returned by the server has HTTP Status Code “429 Too Many Requests”. It includes a Retry-After header with the number of seconds the client is throttled for.\n\nIf you would like your rate limit to be increased, contact us at isd.apiteam@ucl.ac.uk\n\n## API Data Freshness\n\nMuch of the data available from the API is served from cache. Bookings and Timetable data are updated every twenty minutes from UCL, and we update the [Library Study Spaces (Workspaces) API](https://uclapi.com/docs/#workspaces) every two minutes. The `Last-Modified` header will provide the time that the most recent caching operation completed in [RFC 2616](https://stackoverflow.com/a/21121453) format. Endpoints that do not rely on cached data will return the current timestamp in this field instead.\n\nThis allows your application to judge whether the data is stale, or might need refreshing. For example, if a booking is added to the database and the data you are using is more than twenty minutes old, it may be that the booking is not visible to you yet. Consider creating a fresh request in this case.\n\nIf you notice that the `Last-Modified` timestamp you see is unreasonably old, please [get in contact with us](mailto:isd.apiteam@ucl.ac.uk) ASAP to report this as it may indicate very stale data and an issue at our end.\n\n## OAuth\n\nOAuth is a protocol that lets external apps request secure access to private UCL account data without getting your password. This can be done with a \"Sign In With UCL\" button on your website or app which saves UCL users the trouble of registering a new account with you. It also allows your app or website to retrieve a user's personal timetable.\n\nCheck out a JS web app demo [here](https://uclapi-oauth-demo.glitch.me/). The source code for the demo is available [here](https://glitch.com/edit/#!/uclapi-oauth-demo).\n\nFor an example of a mobile app implementation, check out [UCL Assistant](https://github.com/uclapi/ucl-assistant-app) (written in React Native) and the accompanying [UCL Assistant API backend](https://github.com/uclapi/ucl-assistant-api/tree/master/src/oauth) (written in Node.JS).\n\n### Sign In With UCL Button\nIf you want to add a \"Sign In With UCL\" button to your site, which looks like this:\n\n<a href=\"https://uclapi.com/oauth/authorise?client_id=CLIENT_ID&state=STATE\">\n <img width=\"20%\" src=\"https://s3.eu-west-2.amazonaws.com/uclapi-static/SignInWithUCLSmall.png\">\n</a>\n\nyou can copy the following the code below and replace CLIENT_ID and STATE by the `client_id` of your app and a random `state`:\n\n```\n<a href=\"https://uclapi.com/oauth/authorise?client_id=CLIENT_ID&state=STATE\">\n <img src=\"https://s3.eu-west-2.amazonaws.com/uclapi-static/SignInWithUCLSmall.png\">\n</a>\n```\n\n### Scopes\n\nOAuth scopes specify what access your app needs to a UCL user’s account. As an app developer, you set the desired scopes in the API Dashboard. When a user is responding to your authorisation request, the requested scopes will be displayed to the user.\n\n### OAuth Workflow\n\nIf your application uses OAuth, you must set a callback URL in the dashboard. Then the app should follow this procedure:\n\n1. Redirect the user to `https://uclapi.com/oauth/authorise` with `state` and the application’s `client_id` as query parameters.\n\n2. The user logs in with their UCL credentials on the UCL Single Sign-on website (if not logged in already).\n\n3. The user reviews the OAuth scopes requested and either authorises or denies the application's request. If the application is authorised, the callback URL receives `client_id`, `state` (specified in 1.), `result`, and `code`.\n\nIf the application is denied, the callback URL receives `result` and `state`, and no private data will be provided to the application.\n\n4. To obtain a OAuth user token (necessary for retrieving personal data and certain API endpoints), we require `code` (from 3.), `client_id`, and `client_secret`. These should then be sent to `https://uclapi.com/oauth/token`, which will return a response containing `state`, `ok`, `client_id`, `token` (OAuth user token), and `scope` (OAuth scopes the app can access on the user’s behalf).\n\n**Note**: OAuth tokens and general API tokes are different. Whilst general API tokens can be used for all non-personal, generic data (such as room bookings), OAuth tokens must be used with an app's client_secret in order to retrieve personal data for a user. To make things easier, you can use personal OAuth tokens in place of general tokens once a user has logged into your app to retrieve generic data too.\n\n### Tokens\n\nTokens uniquely identify an app that is requesting data from the API.\n\nTokens are a long string variable of numbers and letters. e.g. `uclapi-5d58c3c4e6bf9c-c2910ad3b6e054-7ef60f44f1c14f-a05147bfd17fdb`.\n\nThere are two different kinds of tokens you can work with:\n\n1. Generic Tokens: These are tokens that are used to request non-personal data. These tokens are used between applications and the API to request any sort of data that the app may need that is not tied to a specific student. For example, UCL API’s Room booking service uses tokens to return information about rooms - when they are booked and which UCL rooms are free.\n\n2. OAuth Tokens: This type of token is used when an app requires personal data from users. One of the most common uses of this type of token is when you sign in via UCL on an app. The app will then use a token to request a user’s personal data such as:\n\n- Department\n- Email\n- Full name\n- Given name\n- UPI\n- If they are a student or not\n- Student number (*note:* to get this, you need to tick the relevant scope in the dashboard before a user logs in; more on scopes above).\n\nNote that you can also use OAuth Tokens to access all the same data that generic app tokens can access.\n\nEach token is uniquely generated for each user logging into each app.\n\nPlease note, access to any of this data needs to be approved by the user first.\n\nTo use this type of token for your app, you need to redirect the user to the \"Authorise\" endpoint at: `https://uclapi.com/oauth/authorise` which can be done directly or by including a “Sign in With UCL Button” in your app, such as the one provided below, which links users to the authorisation endpoint with your app’s Client ID (accessible via the dashboard) and a random state number included in the GET parameters.\n\nThe users then sign in with their UCL credentials and, if they authorise your app to use their personal data, a token will be generated which your app can use to get user’s personal data in JSON format from the oauth/user/data. Your application will receive the token at the callback URL you provided in the [Dashboard](https://uclapi.com/dashboard).\n\n## Code Samples\n\nAfter authorisation, calling most of the API endpoints will be fairly similar to each other. Authorisation code samples are provided in their respective descriptions.\n\nIn addition, please find short code examples of how you might call the `/roombookings/equipment` endpoint in your own code (you will find that only the request parameters will change between most different endpoints):\n\nPython:\n```python\nimport requests\n\nparams = {\n \"token\": \"uclapi-5d58c3c4e6bf9c-c2910ad3b6e054-7ef60f44f1c14f-a05147bfd17fdb\",\n \"roomid\": \"433\"\n \"siteid\": \"086\"\n}\n\nr = requests.get(\"https://uclapi.com/roombookings/equipment\", params=params)\nprint(r.json())\n```\n\nShell:\n```shell\ncurl -G https://uclapi.com/roombookings/equipment -d token=uclapi-5d58c3c4e6bf9c-c2910ad3b6e054-7ef60f44f1c14f-a05147bfd17fdb -d roomid=433 -d siteid=086\n```\n\nJavaScript:\n```js\nfetch(\"https://uclapi.com/roombookings/equipment?token=uclapi-5d58c3c4e6bf9c-c2910ad3b6e054-7ef60f44f1c14f-a05147bfd17fdb&roomid=433&siteid=086\")\n .then(response => response.json())\n .then(json => console.log(json));\n```"
contact:
name: API Support
email: isd.apiteam@ucl.ac.uk
url: https://uclapi.com
version: 1.0.0-oas3
termsOfService: https://github.com/uclapi/uclapi/blob/master/backend/uclapi/uclapi/UCLAPIAcceptableUsePolicy.txt
license:
url: https://github.com/uclapi/uclapi/blob/master/LICENSE
name: MIT License
servers:
- url: https://uclapi.com
description: Deployment server
- url: https://staging.ninja
description: Staging server
tags:
- name: OAuth
description: Authenticate and authorise applications for users
paths:
/oauth/authorise:
get:
summary: Authorises a user against the API
description: "This endpoint should be used to authorise a user against the API.\n\nIt will perform a redirect if the user successfully logged in and accepted/denied the request.\n\nSee the below code samples for how you might implement this in your code.\n\nPython:\n```python\n url = \"https://uclapi.com/oauth/authorise/?client_id=123&state=1\"\n\n'''\n in a desktop app, script, or for testing\n'''\nimport webbrowser\nwebbrowser.open_new(url) \n# note that you will also need some way receive the callback\n# this can be done via a web server (e.g. below)\n\n'''\n on a Flask server\n this example covers both redirecting the user to\n the /authorise page and receiving the callback\n'''\nfrom flask import Flask, redirect, request\napp = Flask(__name__)\n\n@app.route('/login')\ndef uclapi_login():\n return redirect(url)\n\n@app.route('/callback')\ndef receive_callback():\n # receive parameters\n result = request.args.get('result', '')\n code = request.args.get('code', '')\n state = request.args.get('state', '')\n # do something with these parameters\n # e.g. request an auth token from /oauth/token\n return\n```\n\nShell:\n```shell\n # linux\n xdg-open \"https://uclapi.com/oauth/authorise/?client_id=123.456&state=1\"\n \n # WSL\n cmd.exe /c start \"https://uclapi.com/oauth/authorise/?client_id=123^&state=1\"\n\n # note that you will also need some way to receive the callback\n```\n\nJavaScript:\n```js\nconst url = \"https://uclapi.com/oauth/authorise/?client_id=123.456&state=1\"\n\n/* in-browser */\nwindow.location.href = url\n// note that you will also need some way to receive the callback\n// this can be done via a web server (e.g. below)\n\n/* Node.JS Express server */\nconst express = require('express')\nconst app = express()\n\napp.get('/login', (req, res) => res.redirect(url))\napp.get('/callback', (req, res) => {\n const {\n result,\n code,\n state\n } = req.params\n // do something with these parameters\n // e.g. request an auth token from /oauth/token\n})\n\napp.listen(3000)\n```"
tags:
- OAuth
parameters:
- name: client_id
in: query
description: Client ID of the authenticating app.
required: true
schema:
type: string
example: 1
- name: state
in: query
description: OAuth (random) state.
required: true
schema:
type: string
example: jAifrW3
responses:
'302':
description: Redirection. See the expected query parameters in the URL description below.
headers:
Location:
description: 'The redirection performed on success OR failure.
The URL will have the following query parameters:
- `client_id` - the Client ID of the authentication app (e.g. `123456`)
- `state` - the same state parameter originally sent as a query parameter to /oauth/authorise (e.g. `jAifrW3`)
- `result` - either `allowed` or `denied` depending on whether the user granted permission
- `code` - a secret code to be used to obtain an OAuth token via the /oauth/token endpoint (e.g. `mysecretcode`)'
schema:
type: string
format: uri
'400':
description: OAuth error
content:
application/json:
schema:
$ref: '#/components/schemas/error'
examples:
Incorrect parameters supplied:
value:
ok: false
error: You did not supply a client_id and a state in your request.
App does not exist for client id:
value:
ok: false
error: You supplied an invalid client_id.
No callback URL set for this app:
value:
ok: false
error: You did not set a callback URL for your app.
UCL has sent incomplete headers:
value:
ok: false
error: UCL sent us incomplete headers. If the issue persists, please contact the UCL API Team.
/oauth/token:
get:
summary: A token will be generated which your app can use to get user’s personal data in JSON format from the OAuthSecurity/user/data.
description: "This endpoint should be used to obtain a token for use in the other API endpoints.\n\nSee the below code samples for how you might implement this in your code.\n\nPython:\n```python\nimport requests\n\nparams = {\n \"client_id\": \"123.456\",\n \"code\": \"1\",\n \"client_secret\": \"secret\",\n}\nr = requests.get(\"https://uclapi.com/oauth/token\", params=params)\nprint(r.json())\n```\n\nShell:\n```shell\ncurl -G https://uclapi.com/oauth/token -d code=mysecretcode -d client_id=123.456 -d client_secret=secret\n```\n\nJavaScript:\n```js\nfetch(\"https://uclapi.com/oauth/token?code=mysecretcode&client_id=123.456&client_secret=secret\")\n .then(response => response.json())\n .then(json => console.log(json));\n```"
tags:
- OAuth
security:
- OAuthSecurity: []
parameters:
- $ref: '#/components/parameters/client_secret'
- name: client_id
in: query
description: Client ID of the authenticating app.
required: true
schema:
type: string
- name: code
in: query
description: Secret code obtained from the authorise endpoint.
required: true
schema:
type: string
responses:
'200':
description: Success
content:
application/json:
schema:
type: object
properties:
ok:
type: boolean
state:
type: string
scope:
type: array
items:
type: string
client_id:
type: string
token:
type: string
'400':
description: OAuth error
content:
application/json:
schema:
$ref: '#/components/schemas/error'
examples:
Incomplete data:
value:
ok: false
error: The client did not provide the requisite data to get a token.
Invalid/Expired code:
value:
ok: false
error: The code received was invalid, or has expired. Please try again.
Non-existent client:
value:
ok: false
error: App has been deleted or the Client ID is invalid.
Invalid client secret:
value:
ok: false
error: Client secret incorrect
/oauth/user/data:
get:
summary: Returns personal data on a student at UCL.
tags:
- OAuth
security:
- OAuthSecurity: []
OAuthToken: []
parameters:
- $ref: '#/components/parameters/client_secret'
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/user_data'
'400':
description: OAuth error
content:
application/json:
schema:
$ref: '#/components/schemas/error'
examples:
'1':
$ref: '#/components/examples/ErrorNoToken'
'2':
$ref: '#/components/examples/ErrorInvalidClientSecret'
'3':
$ref: '#/components/examples/ErrorInvalidToken'
'4':
$ref: '#/components/examples/ErrorThrottling'
'5':
$ref: '#/components/examples/ErrorNoClientSecret'
'6':
$ref: '#/components/examples/ErrorInvalidClientSecret'
'7':
$ref: '#/components/examples/ErrorInactiveToken'
'8':
$ref: '#/components/examples/ErrorPersonalData'
/oauth/user/studentnumber:
get:
summary: You can use the oauth/user/data endpoint to find out whether the user is a student before you call this endpoint. If you call this endpoint and the user is not a student, an error will be returned.
tags:
- OAuth
security:
- OAuthSecurity:
- student_number
OAuthToken: []
parameters:
- $ref: '#/components/parameters/client_secret'
responses:
'200':
description: Success
content:
application/json:
schema:
type: object
properties:
ok:
type: boolean
student_number:
type: string
'400':
description: OAuth error
content:
application/json:
schema:
$ref: '#/components/schemas/error'
examples:
'1':
$ref: '#/components/examples/ErrorNoToken'
'2':
$ref: '#/components/examples/ErrorInvalidClientSecret'
'3':
$ref: '#/components/examples/ErrorInvalidToken'
'4':
$ref: '#/components/examples/ErrorThrottling'
'5':
$ref: '#/components/examples/ErrorNoClientSecret'
'6':
$ref: '#/components/examples/ErrorInvalidClientSecret'
'7':
$ref: '#/components/examples/ErrorInactiveToken'
'8':
$ref: '#/components/examples/ErrorRejectedScope'
User is not a student:
value:
ok: false
error: User is not a student.
components:
examples:
ErrorNoClientSecret:
value:
ok: false
error: No Client Secret provided.
summary: No Client Secret provided
ErrorInvalidClientSecret:
value:
ok: false
error: Client secret incorrect.
summary: Client secret incorrect
ErrorPersonalData:
value:
ok: false
error: Personal data requires OAuth.
summary: OAuth required
ErrorThrottling:
value:
ok: false
error: You have been throttled. Please try again in 10 seconds.
summary: Throttling
ErrorNoToken:
value:
ok: false
error: No token provided.
summary: No token provided
ErrorRejectedScope:
value:
ok: false
error: The token provided does not have permission to access this data.
summary: Weak token scope
ErrorInvalidToken:
value:
ok: false
error: Token is invalid.
summary: Invalid token
ErrorInactiveToken:
value:
ok: false
error: The token is inactive as the user has revoked your app's access to their data.
summary: Inactive token
schemas:
error:
type: object
properties:
ok:
type: boolean
default: false
error:
type: string
user_data:
properties:
ok:
type: boolean
example: 'true'
email:
type: string
example: xxxxxxx@ucl.ac.uk
full_name:
type: string
example: John Doe
department:
type: string
example: Dept of Computer Science
cn:
type: string
example: xxxxxxx
given_name:
type: string
example: John
upi:
type: string
example: jdoe12
scope_number:
type: number
example: 0
is_student:
type: boolean
example: true
parameters:
client_secret:
name: client_secret
in: query
description: Client secret of the authenticating app
required: true
schema:
type: string
securitySchemes:
OAuthToken:
type: apiKey
description: This API requires you to pass your OAuth2 token as a query parameter called 'token'. Use the /authorize and /oauth/token endpoints to authorize a user and get this token.
name: token
in: query
OAuthSecurity:
type: oauth2
description: This API uses OAuth2 with the implicit grant flow. [More info](https://uclapi.com/docs#OAuthSecurity)
flows:
authorizationCode:
authorizationUrl: /authorise
tokenUrl: /token
scopes:
personal_timetable: read user's timetable
student_number: read user's student number