Fetching all Grants Given to a Charity using Python
An example using the Python requests library to fetch all the grants given to an organisation by charity number.
This example script is self-contained and can be run as-is, assuming the requests library is installed.
import time import requests # The base URL of the 360Giving API API_URL = "https://api.threesixtygiving.org/api/v1/" def fetch_grants(charity_number): # List of grants to be added to grants = [] # The API expects organisations to be identified by Org ID (See: https://org-id.guide/about). # Convert an England & Wales charity number into an Org ID by adding the "GB-CHC" prefix. # For more info see: https://org-id.guide/list/GB-CHC # and for charities in other parts of the UK: https://org-id.guide/results?structure=charity&coverage=GB§or=all org_id = "GB-CHC-" + charity_number # Construct the starting URL for grants received by the charity url = API_URL + "org/" + org_id + "/grants_received/" # Note that the results are paginated, it may be necessary to fetch multiple pages. # Loop until all pages are fetched. while url is not None: # Fetch the first page, and set the Accept header to get a JSON response r = requests.get(url, headers={"Accept": "application/json"}) # Check for any HTTP error codes r.raise_for_status() # Convert the JSON response into a dict data = r.json() # For each page, add the page's results to our list of grants grants.extend( data["results"] ) # And th en fetch the next page url = data["next"] # Wait at least half a second between requests to not go over the usage limit time.sleep(0.6) return grants if __name__ == "__main__": # As an example, fetch grants given to England and Wales charity no. 221124 grants = fetch_grants(charity_number="221124") # Print the grant title and funding organisation name for each grant for grant in grants: print(grant["data"]["title"] + " from " + grant["data"]["fundingOrganization"][0]["name"])
Fetching details of an organisation using cURL
Here is an example cURL command to fetch data from the Organisation Detail endpoint. The header Accept: application/json
must be set.
curl --header "Accept: application/json" "https://api.threesixtygiving.org/api/v1/org/GB-CHC-1164883/"
Result (formatted):
{ "self": "https://api.threesixtygiving.org/api/v1/org/GB-CHC-1164883/", "grants_made": "https://api.threesixtygiving.org/api/v1/org/GB-CHC-1164883/grants_made/", "grants_received": "https://api.threesixtygiving.org/api/v1/org/GB-CHC-1164883/grants_received/", "funder": null, "recipient": { "aggregate": { "grants": 29, "currencies": { "GBP": { "avg": 169309.8275862069, "max": 745000, "min": 2000, "total": 4909985, "grants": 29 } } } }, "publisher": null, "org_id": "GB-CHC-1164883", "name": "360 Giving" }