Swift

Try it yourself

Hover over the top right of the code block to copy the code



What Lists are in the chem api?

Use this example to retrieve the set of lists within the chem api

self.resultLabel.text = "Loading..."
        
// set base address
let baseAddress = "https://sandbox.chemadvisor.io/chem/rest/v2/"
        
// set app_key header
let appKey = "your_app_key"
		
// set app_id header
let appId = "your_app_id"
        
// set accept header: "application/xml", "application/json"
let acceptHeader = "application/json"
        
// set resource
let resource = "lists"
        
// set query parameters:q, limit, offset
let q = ""
let limit = 10
let offset = 0
                
let urlComponents = NSURLComponents(string: baseAddress + resource)!
urlComponents.queryItems = [
NSURLQueryItem(name: "q", value: q.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)) as URLQueryItem,
NSURLQueryItem(name: "limit", value: String(limit)) as URLQueryItem,
    NSURLQueryItem(name: "offset", value: String(offset)) as URLQueryItem
    ]
       
let request = NSMutableURLRequest(url: urlComponents.url!)
request.setValue(appKey, forHTTPHeaderField: "app_key")
request.setValue(appId, forHTTPHeaderField: "app_Id")
request.setValue(acceptHeader, forHTTPHeaderField: "Accept")
request.httpMethod = "GET"
        
let session = URLSession.shared
        
let apiCall = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if error != nil {
	print(error?.localizedDescription ?? "Request Error")
} else {
	self.resultLabel.text = String(data: data!, encoding: String.Encoding.utf8)            
	}
}
apiCall.resume()





What Tags are used on Lists within the chem api?

Use this example to retrieve the set of Tags within the chem api

self.resultLabel.text = "Loading..."
        
// set base address
let baseAddress = "https://sandbox.chemadvisor.io/chem/rest/v2/"
        
// set app_key header
let appKey = "your_app_key"
		
// set app_id header
let appId = "your_app_id"
        
// set accept header: "application/xml", "application/json"
let acceptHeader = "application/json"
        
// set resource
let resource = "tags"
        
// set query parameters:q, limit, offset
let q = ""
let limit = 10
let offset = 0
                
let urlComponents = NSURLComponents(string: baseAddress + resource)!
urlComponents.queryItems = [
NSURLQueryItem(name: "q", value: q.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)) as URLQueryItem,
NSURLQueryItem(name: "limit", value: String(limit)) as URLQueryItem,
    NSURLQueryItem(name: "offset", value: String(offset)) as URLQueryItem
    ]
       
let request = NSMutableURLRequest(url: urlComponents.url!)
request.setValue(appKey, forHTTPHeaderField: "app_key")
request.setValue(appId, forHTTPHeaderField: "app_Id")
request.setValue(acceptHeader, forHTTPHeaderField: "Accept")
request.httpMethod = "GET"
        
let session = URLSession.shared
        
let apiCall = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if error != nil {
	print(error?.localizedDescription ?? "Request Error")
} else {
	self.resultLabel.text = String(data: data!, encoding: String.Encoding.utf8)            
	}
}
apiCall.resume()





The power of the 'q'

The 'q' query parameter can be used within a supporting call to search any field within the resource. 'q' supports most of the syntax used by MongoDB db.find()



How do I find the Substance Id (_id) for CAS 50-00-0?

You can use 'q' to lookup a Substance Id (_id) by any Identifier that we support. Some of the Identifiers we support, but are not limited to, are CAS, EC, Annex, ENCS, and Formula

self.resultLabel.text = "Loading..."
        
// set base address
let baseAddress = "https://sandbox.chemadvisor.io/chem/rest/v2/"
        
// set app_key header
let appKey = "your_app_key"
		
// set app_id header
let appId = "your_app_id"
        
// set accept header: "application/xml", "application/json"
let acceptHeader = "application/json"
        
// set resource
let resource = "substances"
        
// set query parameters:q, limit, offset
let q = "{\"identifiers.identifier.value\":\"50-00-0\"}"
let limit = 10
let offset = 0
                
let urlComponents = NSURLComponents(string: baseAddress + resource)!
urlComponents.queryItems = [
NSURLQueryItem(name: "q", value: q.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)) as URLQueryItem,
NSURLQueryItem(name: "limit", value: String(limit)) as URLQueryItem,
    NSURLQueryItem(name: "offset", value: String(offset)) as URLQueryItem
    ]
       
let request = NSMutableURLRequest(url: urlComponents.url!)
request.setValue(appKey, forHTTPHeaderField: "app_key")
request.setValue(appId, forHTTPHeaderField: "app_Id")
request.setValue(acceptHeader, forHTTPHeaderField: "Accept")
request.httpMethod = "GET"
        
let session = URLSession.shared
        
let apiCall = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if error != nil {
	print(error?.localizedDescription ?? "Request Error")
} else {
	self.resultLabel.text = String(data: data!, encoding: String.Encoding.utf8)            
	}
}
apiCall.resume()





How do I find all of the Canada OEL Lists in the chem api?

Using 'q' to get a set of lists by a Tag or Tags

self.resultLabel.text = "Loading..."
        
// set base address
let baseAddress = "https://sandbox.chemadvisor.io/chem/rest/v2/"
        
// set app_key header
let appKey = "your_app_key"
		
// set app_id header
let appId = "your_app_id"
        
// set accept header: "application/xml", "application/json"
let acceptHeader = "application/json"
        
// set resource
let resource = "lists"
        
// set query parameters:q, limit, offset
let q = "{$and: [{\"tags.tag.name\": \"OEL\"}, {\"tags.tag.name\": \"Canada\"}]}"
let limit = 10
let offset = 0
                
let urlComponents = NSURLComponents(string: baseAddress + resource)!
urlComponents.queryItems = [
NSURLQueryItem(name: "q", value: q.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)) as URLQueryItem,
NSURLQueryItem(name: "limit", value: String(limit)) as URLQueryItem,
    NSURLQueryItem(name: "offset", value: String(offset)) as URLQueryItem
    ]
       
let request = NSMutableURLRequest(url: urlComponents.url!)
request.setValue(appKey, forHTTPHeaderField: "app_key")
request.setValue(appId, forHTTPHeaderField: "app_Id")
request.setValue(acceptHeader, forHTTPHeaderField: "Accept")
request.httpMethod = "GET"
        
let session = URLSession.shared
        
let apiCall = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if error != nil {
	print(error?.localizedDescription ?? "Request Error")
} else {
	self.resultLabel.text = String(data: data!, encoding: String.Encoding.utf8)            
	}
}
apiCall.resume()