0% found this document useful (0 votes)
4 views11 pages

API Tutorial - Portail Open Data

This document provides an overview of the API tutorial for a site, detailing authentication requirements, permissions, and pagination methods. It includes examples of how to use the API with command line tools like httpie and jq, as well as instructions for editing and deleting data. Users are cautioned about the finality of modifications and the need for proper authentication to perform write operations.

Uploaded by

manku thimmi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

API Tutorial - Portail Open Data

This document provides an overview of the API tutorial for a site, detailing authentication requirements, permissions, and pagination methods. It includes examples of how to use the API with command line tools like httpie and jq, as well as instructions for editing and deleting data. Users are cautioned about the finality of modifications and the need for proper authentication to perform write operations.

Uploaded by

manku thimmi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Home API Tutorial

API Tutorial
This page describes the behaviour of the free and open API of this site.

Authentication

In order to perform write operations, you must first be authenticated and obtain an API key in your profile settings.

This key must be provided on every call in the X-API-KEY HTTP header.

Permissions

API calls are subject to the same permissions as the web interface.

For example, you must be a member of the organisation to modify any of its datasets.

Pagination
Some methods are paginated and follow the same pagination model. The list of objects is encapsulated in a Page object.

You don't have to calculate the previous and next pages yourself as the URLs are available in the response in the previous_page and
next_page attributes. They will be set to null if there is no previous and/or next page.

Example:

{
"data": [{...}, {...}],
"page": 1,
"page_size": 20,
"total": 10,
"next_page": "[Link]
"previous_page": null
}

Examples

The following examples use the following command line tools: httpie and jq. You can of course use this API with the tools or libraries of your
choice.

Check that httpie is working

Once httpie is installed, you can check that it is working as expected by typing this command in your terminal:

$ http '[Link]

This should return a response like this:


HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
... LOTS OF HEADERS ...

{
"data": [
{

... LOTS OF DATA ...

"name": "Stelareum",
"page": "[Link]
"slug": "stelareum",
"uri": "[Link]
"url": "[Link]
}
],
"next_page": "[Link]
"page": 1,
"page_size": 1,
"previous_page": null,
"total": "10"
}

 

This is very verbose and we don't need all that information right now. That's why we use jq.

Checking jq is working

Once jq is installed, you can check that it is working by typing this command in your terminal:
$ http '[Link] | jq '.data[].name'

This should return a response like this:

"Stelareum"

This is much better! Now that everything is working fine, let's reduce the size of our command line a bit:

$ export API="[Link]

The previous command is now equivalent to the more readable command (don't forget the apostrophes):

$ http $API 'organizations/?page_size=1' | jq '.data[].name'

That's a good start, now let's dive into the API itself. We don't know it yet but we've already retrieved our first organisation.

Browsing and retrieving data

You can retrieve a list of organisations (filtered or not) or a single organisation. When retrieving an access point, the default number of items
per page is 20. Let's retrieve the first 20 organisations via the API:

$ http $API'organizations/' | jq '.data[].name'

"Stelareum"
"Department of Public Works - MMTP"
"NEXXTLAB S.A."
"Legato Team"
Legato Team [Link]" "Legato Team [Link]
"Service National de la Jeunesse" "OpenAgenda
"OpenAgenda" "toto
"toto
"Luxembourg Independent Broadcasting Authority" "Centre de gestion informatique de l'
"Centre de gestion informatique de l'éducation".

It's good to have this list, but what happens if we want to browse the returned organisations? Let's get the first 5 URIs of organisations.

$ http $API 'organizations/?page_size=5' | jq '.data[].uri'

"[Link]
 
"[Link]
"[Link]
"[Link]
"[Link]

Now we are able to retrieve an organisation only via the returned URI.

$ http $API'organizations/5cb1d1ed0f7fb0438df74602/' | jq '.'

That's a lot of data to go through. Let's refine this data, if we only want to extract the metrics:

$ http $API 'organizations/5cb1d1ed0f7fb0438df74602/' | jq '.metrics'


{
"datasets": 0,
"members": 1,
"views": 0,
"followers": 0,
"reuses": 0,
"dataset_views": 0,
"reuse_views": 0,
"resource_downloads": 0,

Or maybe just the names of the members of this organisation:

$ http $API'organizations/5cb1d1ed0f7fb0438df74602/' | jq '.members[].user.last_name'

"Crypto"

It is really up to you to retrieve the relevant data for your project. Feel free to consult jq's tutorial and his manual if you want to browse the API
via the command line in more detail.

Editing and deleting data

Beware, you are entering a danger zone. Modifications and deletions of data via the API are final and we do not provide a sandbox to test
before executing them (yet). Be aware of these responsibilities before using your superpowers.

If you attempt to modify a resource without the authentication token, a 401 error will be returned:
$ http PUT $API'organizations/organization-uri-x/'

HTTP/1.1 401 UNAUTHORIZED


... LOTS OF HEADERS ...

{
"message": "Unauthorized",
"status": 401
}

You must specify your API Key (see above) and use the HTTP header X-API-KEY . If you attempt to modify a resource you do not control, a
400 error will be returned:

$ http PUT $API'organizations/organization-uri-x/' X-API-KEY:[Link]

HTTP/1.1 401 UNAUTHORIZED


... LOTS OF HEADERS ...

{
"message": "Invalid API Key",
"status": 401
}

This is the message you will get if you have specified the wrong API key. This is another potential error message you may encounter.

HTTP/1.1 403 FORBIDDEN


... LOTS OF HEADERS ...
{
"message": "You do not have the permission to modify that object.",
"status": 403
}

This happens if you try to access a resource that you cannot edit with your credentials. If your key is valid you should get something like this:

HTTP/1.1 200 OK
... LOTS OF HEADERS ...

{
...
}

But that doesn't change everything! It's perfectly normal, we forgot to specify the right data to send to the server.

$ http PUT $API'organizations/organization-uri-x/' X-API-KEY:[Link] name="Lorem ipsum"

{
"name": "Lorem ipsum",
"description": "The quick brown fox jumps over the lazy dog."
}

 

The resource has been modified with your new values. Finally, you can delete a resource with the appropriate HTTP verb (note that no rollback
is possible using the API at this time):
$ http DELETE $API'organizations/organization-uri-x/' X-API-KEY:[Link]

HTTP/1.0 204 NO CONTENT


... LOTS OF HEADERS ...

Once done, you can check that it is effective by sending a GET to the previous URL:

$ http GET $API'organizations/organization-uri-x/'

HTTP/1.0 410 GONE


... LOTS OF HEADERS ...

{
"message": "Organization has been deleted",
"status": 410
}

Have a look at our examples showing how to use this API in Java and in Python, as well as the reference documentation.

Actions
› Suggest a modification
Participate Publish a dataset Publish a reuse

Subscribe to our newsletter Follow us


To keep up to date with the latest developments in open data, on social media
subscribe to our newsletter and follow our events.

info@[Link]
Subscribe

The Open Data Support


FAQ API Tutorial

Guide to the use of data API Reference

Guidelines for the publication of open data Contact us udata

udata-front
Request to reuse public data

Open data strategy


Open data roadmap

Project governance

Fact sheets

[Link] [Link] [Link]

Terms Tracking and privacy Accessibility Site map

Unless otherwise stated, all content of this site is available under Creative Commons CC0 license.

You might also like