AMB - Àrea Metropolitana de Barcelona - www.amb.cat

API Opendata Documentation

Back

About the AMB’s Opendata portal

This portal hosts a public dataset about the AMB which can be used with the API.

This page shows how to use the open data API to search for and retrieve data from the sources available on the AMB’s portal.

Organisation of information

The Opendata portal lists the information clustered by collections. Each collection can be searched by its fields or by using the free text search option. Query results can be displayed in several formats to make them easier to use. A collection’s data can be accessed using the URL:

http://opendata.amb.cat/{{collection}}/search

where {{collection}} is the collection’s name. Once accessed, it shows all the data available for the collection. Data is currently available for these collections:

Each collection is associated with a schema which defines the types of fields and the organisation of its contents. The schema of a collection can be viewed using the URL:

http://opendata.amb.cat/{{collection}}/schema

For example, if you look up the news schema you get this information:

{ num: 1, count: 1, items: { _id: "54533d61e57214ab3e8411cc", collectionName: "noticies", schema: { _id: "String", text: "String", entradeta: "String", imatge_principal: { imatge_principal_alternatiu: "String", imatge_principal_peu: "String", imatge_principal_nom: "String", imatge_principal_fitxer: "String", imatge_principal_autor: "String" }, subambit: "String", autor: "String", destacat_titol: "String", tema: "String", lang: "String", ambit: "String", enllacos_relacionats: [ { enllacos_relacionats_descripcio: "String", enllacos_relacionats_extern: "String", enllacos_relacionats_intern: "String", enllacos_relacionats_nom: "String" } ], titol: "String", documents_relacionats: [ { documents_relacionats_fitxer: "String", documents_relacionats_nom: "String", documents_relacionats_descripcio: "String" } ], destacat_entradeta: "String", imatges_relacionades: [ { imatges_relacionades_peu: "String", imatges_relacionades_autor: "String", imatges_relacionades_fitxer: "String", imatges_relacionades_nom: "String", imatges_relacionades_alternatiu: "String" } ], video: { video_alternatiu: "String", video_codi_youtube: "String", video_autor: "String", video_fitxer: "String", video_nom: "String" }, data_noticia: "Date", date_updated: "Date" }, date_updated: "2014-10-31T07:42:25.263Z" } }

Collection searches

Field specific searches

The API allows you to search by a collection’s fields. The searches are made as follows:

http://opendata.amb.cat/{{collection}}/search?{{camp1}}={{valor}}&{{camp2}}={{valor}}&{{camp3}}={{valor}}

where camp1, camp2, camp3 are the names of the fields in the collection. For example, if you want to filter the results by the news title field,you would make the following query:

http://opendata.amb.cat/noticies/search?titol=metropolitans

f the field you need to filter is a nested field (i.e., inside another node), the field name will be the concatenation of all its parent nodes with a "." and the field name. For example, if you need to search for the enllacos_relacionats_nom field you would make the following search:

http://opendata.amb.cat/noticies/search?enllacos_relacionats.enllacos_relacionats_nom=premsa&titol=metropolitans

Free text searches

You can also conduct free text searches by the text fields in each collection. You do this by using the q field in the URL. For example, the following query will search for the word mobility in the collection fields:

http://opendata.amb.cat/noticies/search?q=mobilitat

You can combine this type of search with a field-specific search. An example would be the following:

http://opendata.amb.cat/noticies/search?enllacos_relacionats.enllacos_relacionats_nom=premsa&titol=metropolitans&q=mobilitat

Date field searches

You can make Date field searches using the nomenclature dd/mm/yyyy. An example would be the following search:

http://opendata.amb.cat/noticies/search?enllacos_relacionats.enllacos_relacionats_nom=premsa&titol=metropolitans&q=mobilitat&data_noticia=25/09/2014

You can also search by date range using the symbols <,>,= inside the field value. For example, if you need to retrieve news where the date must be after 25/09/2014, you would make the following call:

http://opendata.amb.cat/noticies/search?enllacos_relacionats.enllacos_relacionats_nom=premsa&titol=metropolitans&q=mobilitat&data_noticia=>25/09/2014

Pagination

The API returns the results grouped in 500 items by default in all your searches. If you look at the header of the results of a search you can identify the elements:
num: number of elements shown in the search.
count: total number of elements in the search.
start: first element of the list shown.

To enable pagination in the API, you have to specify the number of elements you want to retrieve (rows) and which element you want to retrieve the results from (start):

http://opendata.amb.cat/parades_taxi/search?rows=500&start=1000

Sorting

The API allows you to sort the results by a particular field in ascending or descending order. In the search, you need to specify which field you want to sort by and the direction: sort={{camp}},{{asc/desc}}.

http://opendata.amb.cat/noticies/search?titol=metropolitans&sort=autor,asc

Return specific fields in the response

The API allows you to filter the fields returned in the response items. This means you can significantly reduce the search information. For example, if you only need the entry and author fields, you can do this by using the getFields parameter:

http://opendata.amb.cat/noticies/search?q=mobilitat&getFields=entradeta,autor

This feature is only valid for first level fields, i.e., ones that come directly from the item’s root.

Search by different values in the same field

You can search for a field where its value matches one of those specified in the search field. To do this you have to separate each value by the symbol |:

http://opendata.amb.cat/activitats/search?subambit=Residus|Sostenibilitat|Aigua

The example will search for all contents in which the subfield matches any of the query’s values.

Bear in mind that the parameter’s values need to be exactly the same as the values you are searching for (i.e., capital letters, accents, punctuation marks, etc. must match).

Data formats

The API serves the results in JSON format by default. You can also get search information in XML, KML, KMZ and CSV formats. The searches are made as follows:

http://opendata.amb.cat/{{collection}}/search/{{format}}?{{camp1}}={{valor}}&{{camp2}}={{valor}}&{{camp3}}={{valor}}

where the {{format}} field is the type of format you need. A search by XML format would be as follows

http://opendata.amb.cat/noticies/xml/search?enllacos_relacionats.enllacos_relacionats_nom=premsa&titol=metropolitans&q=mobilitat

Other search utilities

Get related fields

Some collections contain contents related to the contents of other collections. For example, the activities collection has contents related to facilities and publications. To see this relationship, you need to add the parameter getRelatedArticles=true to your query:

http://opendata.amb.cat/activitats/search?tema=residus&getRelatedArticles=true

With the previous search, you get the related contents of each item. The following code shows how the related_articles field appears, indicating that a given content is related to two contents of publicacions and eight contents of equipaments_platja.

related_articles: { publicacions: [ { articleId: "754258" }, { articleId: "754270" } ], equipaments_platja: [ { articleId: "421174" }, { articleId: "452516" }, { articleId: "452423" }, { articleId: "493853" }, { articleId: "421165" }, { articleId: "452101" }, { articleId: "452113" }, { articleId: "452451" } ] }

If you want to see related content, you should search in the relevant collection by the articleId field.

Tanca