class: center, middle # .center[SoftLayer API] ### .center[How it works, and what to do when it doesn't] Christopher Gallo, Senior API Developer --- # .center[Anatomy of an API Call] .center[The SoftLayer API has two main urls to send requests to.] - Public: https://api.softlayer.com - Private: https://api.service.softlayer.com Only big difference is the SoftLayer_Resource_Metadata service only exists on the private network API. --- Specify the endpoint type and version ## api.softlayer.com/
rest
/
v3.1
.columnA[ ### Types - REST - SOAP - XMLRPC ### Versions - v3 - v3.1 (Includes the SoftLayer_Search service, and some objectMask improvements) ] .columnB[
] --- API is broken down into "Services", which contain a collection of methods that perform some action.
## api.softlayer.com/rest/v3.1/
SoftLayer_Service
Data returned by the API is broken down into "Datatypes". Services usually have a matching datatype, but not all datatypes have a matching service. - https://sldn.softlayer.com/reference --- .center[
] --- Methods tell the API which action to actually run in a class/service. ## api.softlayer.com/rest/v3.1/SoftLayer_Service/
method
- https://sldn.softlayer.com/reference/services/SoftLayer_Account/getObject/ ```bash $ curl -s -u $SL_USER:$SL_APIKEY \ 'https://api.softlayer.com/rest/v3.1/SoftLayer_Account/getObject.json' {"accountManagedResourcesFlag":false,"accountStatusId":1001,"address1":"4849 Alpha Rd","allowedPptpVpnQuantity":0,"brandId":2,"city":"Dallas", LOTS OF DATA} ``` For XML and SOAP requests, the method, and all other parameters will go into the request payload envelope. --- Some methods require an InitParamter, listed in the documentation. For REST calls, this goes between the Service and Method. For SOAP/XML requests, this is set in the header. ## api.softlayer.com/rest/v3.1/SoftLayer_Service/
initParameter
/method - https://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/getObject/ ```bash $ curl -s -u $SL_USER:$SL_APIKEY \ 'https://api.softlayer.com/rest/v3.1/SoftLayer_Virtual_Guest/100317048/getObject' {"accountId":307608,"createDate":"2020-04-07T04:01:49-06:00","dedicatedAccountHostOnlyFlag":false,"domain":"chechu.com", OTHER DATA} ``` The documentation for this API method says it requires a `SoftLayer_Virtual_GuestInitParameters` header, which means we need to send in a Virtual Guest id as the init parameter. If you get a 404 error on these sort of API calls, check your user has access to the resource, and the resource is active. Cancelled resources are usually not available from the API. --- .center[
] ```bash curl -u $SL_USER:$SL_APIKEY -X POST \ -d '{"parameters": [{"hostname": "testingEdit"}]}' 'https://api.softlayer.com/rest/v3.1/SoftLayer_Virtual_Guest/999999999/editObject.json' ``` --- You can control the format of the returned data by adding .FORMAT to the end of the URI for REST requests. XML requests will return XML data, SOAP requests will return SOAP data. ## api.softlayer.com/SoftLayer_Service/ initParameter/method
.json
### Available formats - json (default for REST) - txt - xml --- # Authentication 1. .lightPurple[Classic API Key: ] - 64 Characters, associated with a SoftLayer user (Your VPN username in the cloud.ibm.com portal) 2. .lightPurple[Cloud.ibm.com: ] - API Key 32 Characters, associated with an IBMid account 3. .lightPurple[Token from Username / Password: ] - Temporary, associated with a SoftLayer user 4. .lightPurple[Bearer Token: ] - Temporary, retrieved from the IBMCloud IAM service Read [Authenticating to the SoftLayer API](https://sldn.softlayer.com/article/authenticating-softlayer-api/) for full details. --- # Metadata service [SoftLayer_Resource_Metadata](https://sldn.softlayer.com/reference/services/SoftLayer_Resource_Metadata/) - Only useable on the https://api.service.softlayer.com endpoint. - No authentication needed. - Only provides basic information about the resource making the API call + `getUserMetadata()` + `getTags()` + `getHostname()` + `getDatacenter` + etc... --- # API Errors 1. .lightPurple[Authentication Headers not found] + Make sure your authentication headers are correctly being sent. 2. .lightPurple[SoftLayer_Exception_ObjectNotFound (404)] + Make sure the user making the API request has access to the resource + Make sure the resource exists, and hasn't been cancelled 3. .lightPurple[SoftLayer_Exception_Public Internal Error] + Requesting too much data can result in this error. Try removing some items from the objectMask or a resultLimit + Some other unexpected error happened. 4. .lightPurple[SoftLayer_Exception_WebService_BadRequest] + The API was not able to parse your request. Make sure there are no odd characters or mismatching brackets. --- # XML Example ```bash $ curl -d @SoftLayer_Account-getObject.xml https://api.softlayer.com/xmlrpc/v3.1/SoftLayer_Account ``` ```xml *SoftLayer_Account-getObject.xml
*
getObject
headers
authenticate
*
username
SL123456
*
apiKey
APIKEYGOESHERE
SoftLayer_ObjectMask
*
mask
mask[id,companyName]
``` --- # XML Response ```xml
companyName
SoftLayer Internal - Development Community
id
307608
``` --- # SOAP Example ```bash curl -d @soapAccountGetObject.soap https://api.softlayer.com/soap/v3.1/SoftLayer_Account ``` ```xml *soapAccountGetObject.soap
*
SL123456
*
APIKEYGOESHERE
*
mask[id,companyName]
*
``` --- # SOAP Response ```xml
SoftLayer Internal - Development Community
307608
``` --- # REST Example ### Basic Template ```bash $> curl -u [username]:[apiKey] \ -d '{"parameters": ["first", "second"]}' \ https://api.[service.]softlayer.com/rest/v3.1/[serviceName]/[initializationParameter]/[methodName]? objectMask=mask[]& objectFilter={} &resultLimit=[offsetValue],[limitValue] ``` ### Real Example ```bash $> curl -u $SL_USER:$SL_APIKEY 'https://api.softlayer.com/rest/v3.1/SoftLayer_Account/getObject.json? objectMask=mask%5Bid%2CcompanyName%5D' {"companyName":"SoftLayer Internal - Development Community","id":307608} ``` *line breaks added for readability only* --- # Simple API calls with CLI tools #### [SLCLI](https://github.com/softlayer/softlayer-python) https://github.com/softlayer/softlayer-python ```bash $ slcli call-api --help Usage: slcli call-api [OPTIONS] SERVICE METHOD [PARAMETERS]... $ slcli call-api SoftLayer_Account getObject --mask="mask[id,companyName]" :.............:............................................: : name : value : :.............:............................................: : companyName : SoftLayer Internal - Development Community : : id : 307608 : :.............:............................................: ``` --- #### [IBMCLOUD SL](https://www.ibm.com/cloud/cli) https://www.ibm.com/cloud/cli ```bash $ ibmcloud.exe sl call-api --help USAGE: ibmcloud.exe sl call-api SERVICE METHOD [OPTIONS] $ ibmcloud.exe sl call-api SoftLayer_Account getObject --mask="mask[id,companyName]" { "companyName": "SoftLayer Internal - Development Community", "id": 307608 } ``` --- # API Gotchas - Sending in method parameters requires the following format for REST requests ```json {"parameters": ["first", {"second_is_object":{"id":1234}}]} ``` - Paramters are NOT NAMED in the request, they go in order as listed on sldn.softlayer.com - XMLRPC returns integers as signed 32 bit, so some large numbers might overflow - Adding a LOCAL property to an object mask removes all default properties - Its very easy to request too much data from the API. Be mindful of how many properties you are asking for in a single request. - Its better to make a lot of small API requests than one large request. --- # .center[The End] .center[for more detailed information and documentation] ## .center[https://sldn.softlayer.com/article/]