How to Manipulate Tags (CRUD)
Overview
The following page will provide examples of bash script demonstrating how to use the CRUD (Create, Read, Update, Delete) endpoints for tags in the REST v11 API.
Authentication
First, you will need to authenticate to the Sugar API. An example is shown below:
curl -X POST -H Cache-Control:no-cache -d '{
"grant_type":"password",
"client_id":"sugar",
"client_secret":"",
"username":"admin",
"password":"password",
"platform":"custom_api"
}' http://<site_url>/rest/v11/oauth2/token
More information on authenticating can be found in the How to Authenticate and Log Out example and /oauth2/logout POST endpoint documentation.
Creating Tags
Once you get oauth_token you would need to use it in the following API Calls to create tags.
curl -X POST -H OAuth-Token:<access_token> -H Cache-Control:no-cache -d '{
"name":"Test Name",
}' http://<site_url>/rest/v11/Tags
More information on this API endpoint can be found in the /<module> POST documentation.
Response
{
"id": "12c6ee48-1000-11e8-8838-6a0001bcacb0",
"name": "Tag Name",
"date_entered": "2018-02-12T15:21:52+01:00",
"date_modified": "2018-02-12T15:21:52+01:00",
"modified_user_id": "1",
"modified_by_name": "Administrator",
"modified_user_link": {
"full_name": "Administrator",
"id": "1",
"_acl": {
"fields": {
"pwd_last_changed": { "write": "no", "create": "no" },
"last_login": { "write": "no", "create": "no" }
},
"delete": "no",
"_hash": "08b99a97c2e8d792f7a44d8882b5af6d"
}
},
"created_by": "1",
"created_by_name": "Administrator",
"created_by_link": {
"full_name": "Administrator",
"id": "1",
"_acl": {
"fields": {
"pwd_last_changed": { "write": "no", "create": "no" },
"last_login": { "write": "no", "create": "no" }
},
"delete": "no",
"_hash": "08b99a97c2e8d792f7a44d8882b5af6d"
}
},
"description": "",
"deleted": false,
"name_lower": "tag name",
"following": "",
"my_favorite": false,
"locked_fields": [],
"source_id": "",
"source_type": "",
"source_meta": "",
"assigned_user_id": "1",
"assigned_user_name": "Administrator",
"assigned_user_link": {
"full_name": "Administrator",
"id": "1",
"_acl": {
"fields": {
"pwd_last_changed": { "write": "no", "create": "no" },
"last_login": { "write": "no", "create": "no" }
},
"delete": "no",
"_hash": "08b99a97c2e8d792f7a44d8882b5af6d"
}
},
"_acl": { "fields": {} },
"_module": "Tags"
}
Creating Records with Tags
You can also create tags on initial record creation. All you need to do populate the tag field as an array when creating a record. Here is an example that demonstrates using the /<module> POST
endpoint.
curl -X POST -H OAuth-Token:<access_token> -H Cache-Control:no-cache -d '{
name: "Test Record",
tag:["First Tag", "Second Tag"]
}' http://<site_url>/rest/v11/Accounts
More information on this API endpoint can be found in the /<module> POST documentation.
Response
The data sent to the server is shown below:
{
"id": "ea507760-0ffd-11e8-bcf5-6a0001bcacb0",
"name": "Test Record",
"date_entered": "2018-02-12T15:06:25+01:00",
"date_modified": "2018-02-12T15:06:25+01:00",
"modified_user_id": "1",
"modified_by_name": "Administrator",
"modified_user_link": {
"full_name": "Administrator",
"id": "1",
"_acl": {
"fields": {
"pwd_last_changed": { "write": "no", "create": "no" },
"last_login": { "write": "no", "create": "no" }
},
"delete": "no",
"_hash": "08b99a97c2e8d792f7a44d8882b5af6d"
}
},
"created_by": "1",
"created_by_name": "Administrator",
"created_by_link": {
"full_name": "Administrator",
"id": "1",
"_acl": {
"fields": {
"pwd_last_changed": { "write": "no", "create": "no" },
"last_login": { "write": "no", "create": "no" }
},
"delete": "no",
"_hash": "08b99a97c2e8d792f7a44d8882b5af6d"
}
},
"description": "",
"deleted": false,
"facebook": "",
"twitter": "",
"googleplus": "",
"account_type": "",
"industry": "",
"annual_revenue": "",
"phone_fax": "",
"billing_address_street": "",
"billing_address_street_2": "",
"billing_address_street_3": "",
"billing_address_street_4": "",
"billing_address_city": "",
"billing_address_state": "",
"billing_address_postalcode": "",
"billing_address_country": "",
"rating": "",
"phone_office": "",
"phone_alternate": "",
"website": "",
"ownership": "",
"employees": "",
"ticker_symbol": "",
"shipping_address_street": "",
"shipping_address_street_2": "",
"shipping_address_street_3": "",
"shipping_address_street_4": "",
"shipping_address_city": "",
"shipping_address_state": "",
"shipping_address_postalcode": "",
"shipping_address_country": "",
"parent_id": "",
"sic_code": "",
"duns_num": "",
"parent_name": "",
"member_of": {
"name": "",
"id": "",
"_acl": { "fields": [], "_hash": "654d337e0e912edaa00dbb0fb3dc3c17" }
},
"campaign_id": "",
"campaign_name": "",
"campaign_accounts": {
"name": "",
"id": "",
"_acl": { "fields": [], "_hash": "654d337e0e912edaa00dbb0fb3dc3c17" }
},
"following": true,
"my_favorite": false,
"tag": [
{
"id": "ea69c120-0ffd-11e8-b5f6-6a0001bcacb0",
"name": "First Tag",
"tags__name_lower": "first tag"
},
{
"id": "eafb28e0-0ffd-11e8-8d80-6a0001bcacb0",
"name": "Second Tag",
"tags__name_lower": "second tag"
}
],
"locked_fields": [],
"assigned_user_id": "",
"assigned_user_name": "",
"assigned_user_link": {
"full_name": "",
"id": "",
"_acl": { "fields": [], "_hash": "654d337e0e912edaa00dbb0fb3dc3c17" }
},
"team_count": "",
"team_count_link": {
"team_count": "",
"id": "1",
"_acl": { "fields": [], "_hash": "654d337e0e912edaa00dbb0fb3dc3c17" }
},
"team_name": [
{
"id": "1",
"name": "Global",
"name_2": "",
"primary": true,
"selected": false
}
],
"email": [],
"email1": "",
"email2": "",
"invalid_email": "",
"email_opt_out": "",
"email_addresses_non_primary": "",
"calculated_c": "",
"_acl": { "fields": {} },
"_module": "Accounts"
}
Reading/Retrieving Tags
Next, we can retrieve the records using /Tags/:record GET
endpoint. Here is the example of retrieving the tag record that we created.
curl -H OAuth-Token: -H Cache-Control:no-cache http://<site_url>/rest/v11/Tags/12c6ee48-1000-11e8-8838-6a0001bcacb0
More information on this API endpoint can be found in the /<module>/:record GET documentation.
Response
{
"id": "12c6ee48-1000-11e8-8838-6a0001bcacb0",
"name": "Tag Name",
"date_entered": "2018-02-12T15:21:52+01:00",
"date_modified": "2018-02-12T15:21:52+01:00",
"modified_user_id": "1",
"modified_by_name": "Administrator",
"modified_user_link": {
"full_name": "Administrator",
"id": "1",
"_acl": {
"fields": {
"pwd_last_changed": { "write": "no", "create": "no" },
"last_login": { "write": "no", "create": "no" }
},
"delete": "no",
"_hash": "08b99a97c2e8d792f7a44d8882b5af6d"
}
},
"created_by": "1",
"created_by_name": "Administrator",
"created_by_link": {
"full_name": "Administrator",
"id": "1",
"_acl": {
"fields": {
"pwd_last_changed": { "write": "no", "create": "no" },
"last_login": { "write": "no", "create": "no" }
},
"delete": "no",
"_hash": "08b99a97c2e8d792f7a44d8882b5af6d"
}
},
"description": "",
"deleted": false,
"name_lower": "tag name",
"following": "",
"my_favorite": false,
"locked_fields": [],
"source_id": "",
"source_type": "",
"source_meta": "",
"assigned_user_id": "1",
"assigned_user_name": "Administrator",
"assigned_user_link": {
"full_name": "Administrator",
"id": "1",
"_acl": {
"fields": {
"pwd_last_changed": { "write": "no", "create": "no" },
"last_login": { "write": "no", "create": "no" }
},
"delete": "no",
"_hash": "08b99a97c2e8d792f7a44d8882b5af6d"
}
},
"_acl": { "fields": {} },
"_module": "Tags"
}
Updating Tags
You can update a tag using the /Tags/:record PUT
endpoint. In this example, we are going to update the Tag record that we created in the Creating Tags section and change its name to "Renamed Tag Name".
curl -X PUT -H OAuth-Token:<access_token> -H Cache-Control:no-cache -d '{
"name":"Renamed Tag Name"
}' http://<site_url>/rest/v11/Tags/12c6ee48-1000-11e8-8838-6a0001bcacb0
More information on this API endpoint can be found in the /<module>/:record PUT documentation.
Response
{
"id": "12c6ee48-1000-11e8-8838-6a0001bcacb0",
"name": "Renamed Tag Name",
"date_entered": "2018-02-12T15:21:52+01:00",
"date_modified": "2018-02-12T16:07:18+01:00",
"modified_user_id": "1",
"modified_by_name": "Administrator",
"modified_user_link": {
"full_name": "Administrator",
"id": "1",
"_acl": {
"fields": {
"pwd_last_changed": { "write": "no", "create": "no" },
"last_login": { "write": "no", "create": "no" }
},
"delete": "no",
"_hash": "08b99a97c2e8d792f7a44d8882b5af6d"
}
},
"created_by": "1",
"created_by_name": "Administrator",
"created_by_link": {
"full_name": "Administrator",
"id": "1",
"_acl": {
"fields": {
"pwd_last_changed": { "write": "no", "create": "no" },
"last_login": { "write": "no", "create": "no" }
},
"delete": "no",
"_hash": "08b99a97c2e8d792f7a44d8882b5af6d"
}
},
"description": "",
"deleted": false,
"name_lower": "renamed tag name",
"following": "",
"my_favorite": false,
"locked_fields": [],
"source_id": "",
"source_type": "",
"source_meta": "",
"assigned_user_id": "1",
"assigned_user_name": "Administrator",
"assigned_user_link": {
"full_name": "Administrator",
"id": "1",
"_acl": {
"fields": {
"pwd_last_changed": { "write": "no", "create": "no" },
"last_login": { "write": "no", "create": "no" }
},
"delete": "no",
"_hash": "08b99a97c2e8d792f7a44d8882b5af6d"
}
},
"_acl": { "fields": {} },
"_module": "Tags"
}
Deleting Tags
Finally, we will delete the record we created using /Tags/:record DELETE
API request.
curl -X DELETE -H OAuth-Token:<access_token> -H Cache-Control:no-cache http://<site_url>/rest/v11/Tags/12c6ee48-1000-11e8-8838-6a0001bcacb0
More information on this API endpoint can be found in the /<module>/:record DELETE documentation.
Response
{"id":"12c6ee48-1000-11e8-8838-6a0001bcacb0"}