Skip to content

Instantly share code, notes, and snippets.

@mheadd
Last active October 12, 2023 07:53
Show Gist options
  • Save mheadd/a9bb37a51972cbff8ae0 to your computer and use it in GitHub Desktop.
Save mheadd/a9bb37a51972cbff8ae0 to your computer and use it in GitHub Desktop.
Sample API calls for creating, updating and deleting things in CKAN via the CKAN API.

Get package list

~$ curl http://www.civicdata.com/api/action/package_list

Get package details

~$ curl http://www.civicdata.com/api/action/package_show?id={package_id}

Upload a file to storage

~$ curl http://www.civicdata.com/api/storage/auth/form/testdata/test.csv -H "Authorization: <API-KEY>"
{
  "fields": [
    {
      "value": "testdata/test.csv",
      "name": "key"
    }
  ],
  "action": "/storage/upload_handle"
}
~$ curl http://test.civicdata.com/storage/upload_handle \
-H "Authorization: <API-KEY>" \
--form file=@my-test-file.csv \
--form "key=testdata/test.csv"

Create a new resource

~$ curl http://www.civicdata.com/api/3/action/resource_create \
-d '{"package_id": "2811de81-f77a-48bb-a5d6-a8c99a8705e9", "url": "http://civicdatatest.cloudapp.net/storage/f/testdata/test.csv", "name": "My Test Data"}' \
-H "Authorization: <API-KEY>"
{
  "result": {
    "resource_type": null,
    "revision_id": "bb289686-cb98-413f-8220-60028c01329e",
    "position": 1,
    "mimetype_inner": null,
    "webstore_url": null,
    "url": "http:// http://civicdatatest.cloudapp.net/storage/f/testdata/test.csv",
    "created": "2014-05-27T21:00:42.947898",
    "hash": "",
    "state": "active",
    "size": null,
    "id": "86a702f4-2d3a-4ee9-b375-26bc2800cba5",
    "webstore_last_updated": null,
    "revision_timestamp": "2014-05-27T21:00:42.922253",
    "cache_last_updated": null,
    "resource_group_id": "02233ab0-f540-412e-8cca-02685c4bb3a6",
    "description": "",
    "format": "",
    "tracking_summary": {
      "recent": 0,
      "total": 0
    },
    "last_modified": null,
    "url_type": null,
    "mimetype": null,
    "cache_url": null,
    "name": "My Test Data"
  },
  "success": true,
  "help": "Appends a new resource to a datasets list of resources.\n\n    :param package_id: id of package that the resource needs should be added to.\n    :type package_id: string\n    :param url: url of resource\n    :type url: string\n    :param revision_id: (optional)\n    :type revisiion_id: string\n    :param description: (optional)\n    :type description: string\n    :param format: (optional)\n    :type format: string\n    :param hash: (optional)\n    :type hash: string\n    :param name: (optional)\n    :type name: string\n    :param resource_type: (optional)\n    :type resource_type: string\n    :param mimetype: (optional)\n    :type mimetype: string\n    :param mimetype_inner: (optional)\n    :type mimetype_inner: string\n    :param webstore_url: (optional)\n    :type webstore_url: string\n    :param cache_url: (optional)\n    :type cache_url: string\n    :param size: (optional)\n    :type size: int\n    :param created: (optional)\n    :type created: iso date string\n    :param last_modified: (optional)\n    :type last_modified: iso date string\n    :param cache_last_updated: (optional)\n    :type cache_last_updated: iso date string\n    :param webstore_last_updated: (optional)\n    :type webstore_last_updated: iso date string\n\n    :returns: the newly created resource\n    :rtype: dictionary\n\n    "
}

Insert data into the CKAN Datastore

~$ curl -X POST http://www.civicdata.com/api/3/action/datastore_create \
-H "Authorization: <API-KEY>" \
-d '{"resource_id": "86a702f4-2d3a-4ee9-b375-26bc2800cba5", "fields": [ {"id": "a"}, {"id": "b"} ], "records": [ { "a": 1, "b": "XYZ"}, {"a": 2, "b": "ZZZ"} ], "primary_key": ["a"], "force": "True"}'

Update data in the CKAN Datastore

~$ curl -X POST http://www.civicdata.com/api/3/action/datastore_upsert \
-H "Authorization: <API-KEY>" \
-d '{"resource_id": "86a702f4-2d3a-4ee9-b375-26bc2800cba5", "method": "upsert", "records": [ { "a": 1, "b": "XYZXYZXYZ"}, {"a": 2, "b": "ZZZZZZZZZ"} ],"force": "True"}'

Update a resource

~$ curl -X POST http://www.civicdata.com/api/3/action/resource_update \
-H "Authorization: <API-KEY>" \
-d '{"id": "86a702f4-2d3a-4ee9-b375-26bc2800cba5", "url": "http://civicdatatest.cloudapp.net/storage/f/testdata/test.csv", "name": "My Updated Test Data"}'
{
  "result": {
    "resource_type": "file",
    "revision_id": "19bd426b-1c4a-4e30-af0b-06089f11ba01",
    "position": 1,
    "mimetype_inner": null,
    "webstore_url": null,
    "url": "http://civicdatatest.cloudapp.net/storage/f/testdata/test.csv",
    "created": "2014-05-27T21:00:42.947898",
    "hash": "",
    "state": "active",
    "size": null,
    "id": "86a702f4-2d3a-4ee9-b375-26bc2800cba5",
    "webstore_last_updated": null,
    "revision_timestamp": "2014-05-27T21:12:53.463324",
    "cache_last_updated": null,
    "resource_group_id": "02233ab0-f540-412e-8cca-02685c4bb3a6",
    "description": "",
    "format": "",
    "tracking_summary": {
      "recent": 0,
      "total": 0
    },
    "last_modified": null,
    "url_type": null,
    "mimetype": null,
    "cache_url": null,
    "name": "My Updated Test Data"
  },
  "success": true,
  "help": "Update a resource.\n\n    To update a resource you must be authorized to update the dataset that the\n    resource belongs to.\n\n    For further parameters see ``resource_create()``.\n\n    :param id: the id of the resource to update\n    :type id: string\n\n    :returns: the updated resource\n    :rtype: string\n\n    "
}

Delete data from the CKAN Datastore.

~$ curl -X POST http://www.civicdata.com/api/3/action/datastore_delete \
-H "Authorization: <API-KEY>" \
-d '{"resource_id": "86a702f4-2d3a-4ee9-b375-26bc2800cba5", "force":"True"}'
{
  "result": {
    "__extras": {
      "force": "True"
    },
    "resource_id": "86a702f4-2d3a-4ee9-b375-26bc2800cba5"
  },
  "success": true,
  "help": "Deletes a table or a set of records from the DataStore.\n\n    :param resource_id: resource id that the data will be deleted from. (optional)\n    :type resource_id: string\n    :param filters: filters to apply before deleting (eg {\"name\": \"fred\"}).\n                   If missing delete whole table and all dependent views. (optional)\n    :type filters: dictionary\n\n    **Results:**\n\n    :returns: Original filters sent.\n    :rtype: dictionary\n\n    "
}
@mrnugraha
Copy link

Hi Mark Headd,
thank you for your article gist, I try Create a new resource with file upload, can you help me code curl for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment