• Stars
    star
    133
  • Rank 264,066 (Top 6 %)
  • Language
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

👮 REST API guidelines created for the Watson Developer Cloud services

REST API Guidelines

These API guidelines are used to guide design of the IBM's Watson Developer Cloud services, but may provide insight for other REST APIs as well.

We also have a code-style and repository guidelines, take a look 😎

Table of Content

Naming

The words used should match the users' terminology.

All names specified in path and query parameters, resource names, and JSON input and output fields and pre-defined values must use "snake_case" and must not use abbreviations or acronyms.

Good:

  • classifier_id
  • status_description
  • generate_visualization

Bad:

  • observeResult
  • lang
  • DateRange
  • vad_score

Exceptions (cases when we use abbreviations or acronyms):

  • en, en-us. (Since there's an established convention for language codes, we use these.)
  • ibm, http, etc. (When an acronym or abbreviation is more common than the full name, we use the shorter name instead.)

JSON vs CSV vs XML

APIs should support JSON for non-binary data input and output, and may also support CSV or XML when desired (although it's not usually recommended), using the Content-Type (for input) and Accept (for output) headers to specify formats. Formats should default to JSON when not specified.

JSON Structure

Avoid Anonymous Arrays

For JSON requests and responses, APIs should not use top-level, anonymous arrays. Wrapping the arrays in a JSON object makes it possible to add additional fields later.

Good:

  • {"items": ["item 1", "item 2"]}

Bad:

  • ["item 1", "item 2"]

Avoid Dynamic Keys

The keys in JSON objects should be static strings, not dynamic user-provided values. (Parsing a JSON object that represents a hash-map of user-provided keys and values is difficult in JavaScript, requiring the filtering out of native properties using hasOwnProperty().)

Good:

  • {"user_key": "my user key", "user_value": "my user value"}

Bad:

  • {"my user key": "my user value"}

Pretty-Printed Responses

Results should be pretty printed by default. The data overhead, with gzip enabled, is negligible. http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api#pretty-print-gzip

Multiple Languages

When APIs support content in multiple languages (English or Spanish, for example), the language should be specified using the Content-Language header field, with language values following RFC 1766, for example "en" or "es-ES".

When handling input data where multiple parts of the input can be in multiple languages, a JSON field of "language" can be used (overriding the header field, if it exists), for example: {"profiles": [{"text": "hello", "language": "en"}, {"text": "hola", "language": "es"}]}

User-Created Resources

User-created resources should be created by POSTing to /resource_name, and accessed at /resource_name/resource_id, where resource_id is generated by the service.

Resource IDs

User-created resources IDs should be generated by the service (not specified by the users), and be globally unique (not just unique to the user). For a friendly, non-unique identifier, resources should include a user-specified "name" field.

IDs can be GUIDs (9fd3b33a-5b7d-4e79-9bca-96054e3556f8), or shorter unique strings (47C164-nlc-243), and can optionally include a truncated, sanitized version of the user-specified "name" field to make the id more readable (i.e. 47C164243-my_weather_classifier).

Asynchronous Operations

For operations that take more than ~15 seconds, users should initial an operation with a POST, which should return the ID of the resource for the newly created resource / asynchronous job.

For example:

POST https://api.us-south.natural-language-classifier.watson.cloud.ibm.com/v1/classifiers responds with:

{
  "classifier_id": "47C164-nlc-243",
  "name": "weather",
  "language": "en",
  "created": "2015-08-24T18:42:25.324Z",
  "url": "https://api.us-south.natural-language-classifier.watson.cloud.ibm.com/v1/classifiers/47C164-nlc-243",
  "status": "Training",
  "status_description": "The classifier instance is in its training phase, not yet ready to accept classify requests"
}

To retrieve the status of the operation, or the results (when the operation completes), the same url should be used, for example:

GET https://api.us-south.natural-language-classifier.watson.cloud.ibm.com/v1/classifiers/47C164-nlc-243

If the asychronous operation results are meant to be ephemeral, instead of a persistent resource, then users should delete the resource after retrieval:

DELETE https://api.us-south.natural-language-classifier.watson.cloud.ibm.com/v1/classifiers/47C164-nlc-243

External input and output

For very large inputs or outputs, a service may also accept Object Storage pointers and credentials to asynchronously read in very large input and write out very large output.

Multi-lingual Support

Language codes should be specified in input and output as "en", "en-US". Left unspecified the language should default to "en".

Language codes without country dialects ("en", "es") should use a default dialect for that language ("en-US", "es-ES"), if there are multiple dialects available or the single available language model is dialect specific. The response should include the language that was used, separate from the language that was specified. (Exception: Chinese should require a dialect to differentiate between "zh-CN" and "zh-TW", and not provide a default for "zh".)

Dialects that don't have a direct match should match more generic or default models, when the models used are marked as supporting the whole language code. For example, "en-GB" could match an "en" or "en-US" model that was marked as supporting en-*.

The standard http header content-language should be supported to specify the input language.

A "language" field in JSON input can also be supported, and should take precedence over the header. The JSON "language" field may apply to individual parts of input, while the header provides a default language value for all of the input in the request.

Dates

Dates returned in responses should be formatted using ISO 8601 like: yyyy-MM-dd'T'HH:mm:ss.SSS'Z', for example 2015-08-24T18:42:25.324Z. All date/times returned in responses should be in the UTC time zone (that is why the 'Z' is required literally).

Date taken as input should be formatted the same, with the seconds and time fields optional (for example: 2015-08-24 or 2015-08-24T18:42). Date / times taken as input should be flexible in the time zones they accept (any valid time zone should be accepted), for example: 2015-08-24T18:42:25.324-0400.

Errors

Error Status Codes

5xx errors should be only for server errors and uptime failures — there should be no repeatable way for users to generate 5xx error codes. 4xx errors should be used when the failure is a result of an incorrect or unsupported user input.

W3 HTTP status codes

Error JSON Objects

Examples:

  • {"code": 404, "error": "Not found"}
  • {"code": 400, "error": "Missing text", "description": "The required 'text' parameter is missing."}

GET vs POST vs PUT

GET methods should be safe — they should not have side effects — and idempotent (they should return the same result until the underlying resource changes).

POST methods need not be safe or idempotent, but should avoid query parameters. Methods that are safe should use GET instead, unless parameters larger than ~4KB are required, in which case equivalent GET and POST

PUT methods should be idempotent (calling the PUT method multiple times with the same parameters should be indistinguishable from calling it once) and should fully replace the resource and all sub-resources. PUT should also avoid query parameters.

Note: We've avoided PATCH so far because of the two competing PATCH formats, JSON Patch violates multiple of our other API guidelines (top-level arrays, abbreviations), while JSON Merge lacks the expressiveness to remove fields.

Common API Problems

GET vs POST vs PUT

Are all GET methods safe, and do POST calls need to be POST?

Quoted numbers

Are any of the numeric fields accidentally returned as strings? ("value": "0.923" instead of "value": 0.923)

Naming

Do all identifiers (resource names, parameter names, JSON field names) use snake_case and not use acronyms or abbreviations?

Anonymous JSON arrays

When input or output JSON structures contain top level anonymous arrays, for example: ["item 1", "item 2"] instead of {"items": ["item 1", "item 2"]} they are harder to extend later.

Watson Developer Cloud Guidelines

(This part of the guidelines are only applicable to IBM Watson's Developer Cloud Services.)

Versioning

Services should include a major version in the path (/v1/) and a minor version as a required query parameter that takes a date: ?version=2015-11-24 (inspired by the Foursquare model of date-versioning). This minor version can then be used to control the behavior of minor breaking changes, so that when an output field name, status code, or other change is made users are not affected until they update their version. (Changes to underlying models are not considered breaking and are not controlled by the version parameter.)

Developers should never pass the current date as a version, and should instead use the latest version available when writing code and periodically check for changes and update their code and version to take advantage of them.

What counts as "breaking"

At a high level, a "breaking" change is a change that would cause "reasonable" code to stop working. What's "reasonable" is subjective, but examples of changes we consider non-breaking include:

  • Addition of output fields
  • Addition of (optional) input parameters
  • Changes to underlying models and algorithms that may result in different results and values
  • Changes to string values (in most cases; dates and other structured strings are exceptions to this)

Changes we consider breaking include:

  • Removing output fields
  • Addition of a required input parameter
  • Changes to parameter default values
  • Change of field names
  • Change of status codes (ideally; historically we've made changes between 4xx and other 4xx, and 2xx and other 2xx, without considering them breaking)

Date versioning FAQ

Why make the version date required? Wouldn't it be simpler to make it optional?

The main reason not to provide a default version is that the only stable behavior would have to be defaulting to the oldest version. We want developers to target the newest version at the date they write their code, but in a way that won't break their code when we make breaking changes. (FourSquare stopped accepting requests without version dates, likely for the same reasons.)

The exception is that when adding version dates to an existing public API major version that did not previously have version dates, the introduction of the version parameter would have to be optional to avoid being a breaking change.

My service would never use this, since it would be too expensive to maintain multiple versions.

One of the most common of these changes are design defects that slipped unnoticed into a public release, such as accidental quotation of numbers, unnecessary output fields, field names that don't follow the API's naming patterns, status codes that don't follow the REST conventions (especially in error scenarios), incorrect date formats, error response field names that match the rest of the API and platform, and default parameter values that cause usability or performance problems. (Each of these issues has occurred at least once in public Watson Developer Cloud APIs.) Stripe's API changelog has more examples of the types of API fixes and evolutions that are useful to be able to make.

In the absence of version dates, these defects persist in the API until the next major version; with version dates, they can be addressed for new users without breaking early adopters' code. With version dating, a single major version of the service can conditionally follow the new API design. Even if a service doesn't intend to make breaking changes, historically the need for them has been common.

Should a service reject calls where the version date matches the current date or a future date?

The current answer to this is no, although the reasons for this are less strong than those around making version dates required, and are more implementation centric than API centric. One reason to not reject requests for future dates is to make it easier to test and deploy features that will be enabled in a future release; internal testing of a new change that will take effect at a future date is simpler when there isn't code (or production-conditioned code) that compares the version date to the current date.

Rejecting the current date is even less desirable, since it might be a valid date, and the developer pattern of targeting the API by given the date the code was written is valid. From a service implementation standpoint, not comparing the version date parameter is the fastest and simplest strategy. From a users' standpoint, however, the behavior of passing a future version date should be considered undefined, since a new release with breaking changes might be released (or we may switch to rejecting future version dates).

Bluemix Lifecycle

Provisioning or binding a service in Bluemix should not automatically create resources within the service, and users shouldn't need to create two instances of a service on Bluemix to obtain more resources. Instead, creation and deletion of resources should be through REST APIs. (Deprovisioning a service should remove any resources that are no longer accessible.)

Metadata

To allow developers to keep track of metadata associated with their REST calls (like customer ids, testing flags), services should accept the header X-Watson-Metadata, and echo this as a response header. Developers are encouraged to use semi-colon separated values and name=value pairs, such as: X-Watson-Metadata: customer_id=1242;testing;platform=iOS

URL Basepaths

Services should follow the pattern:

<tool>.<type>.<region>.<service-name>.<env-id>.watson.cloud.ibm.com

Where <tool> is voice, api, or blank for tooling.

Where <type> is private, direct, dedicated, or blank for public.

Where <region> is us-south, us-east, eu-gb, eu-de, au-syd, jp-tok, kr-seo.

Where <service-name> is the full service name.

Where <env-id> is dev, test, preprod or blank for production.

For example:

https://api.us-south.assistant.watson.cloud.ibm.com

https://api.us-east.discovery.watson.cloud.ibm.com

Other API Design Resources

Best Practices for Designing a Pragmatic RESTful API - Overview of basic REST API design

18F API Standards - REST API guidelines from Whitehouse technology team

Move Fast, Don't Break Your API - Talk about Stripe's use of version dates

Microsoft API Guidelines - Microsoft's detailed API guidelines

More Repositories

1

node-sdk

☄️ Node.js library to access IBM Watson services.
TypeScript
1,482
star
2

python-sdk

🐍 Client library to use the IBM Watson services in Python and available in pip as watson-developer-cloud
Python
1,452
star
3

speech-to-text-nodejs

🎤 Sample Node.js Application for the IBM Watson Speech to Text Service
JavaScript
1,086
star
4

swift-sdk

📱 The Watson Swift SDK enables developers to quickly add Watson Cognitive Computing services to their Swift applications.
Swift
880
star
5

java-sdk

🥇 Java SDK to use the IBM Watson services.
Java
586
star
6

unity-sdk

🎮 Unity SDK to use the IBM Watson services.
C#
570
star
7

personality-insights-nodejs

📊 Sample Nodejs Application for the IBM Watson Personality Insights Service
JavaScript
558
star
8

visual-recognition-coreml

Classify images offline using Watson Visual Recognition and Core ML
Swift
487
star
9

assistant-simple

A simple sample application demonstrating the Watson Assistant api.
JavaScript
482
star
10

tone-analyzer-nodejs

Sample Node.js Application for the IBM Tone Analyzer Service
CSS
452
star
11

text-to-speech-nodejs

This is a deprecated Watson Text to Speech Service Demo. A link to the newly supported demo is below
JavaScript
346
star
12

speech-javascript-sdk

Library for using the IBM Watson Speech to Text and Text to Speech services in web browsers.
JavaScript
256
star
13

node-red-labs

Node-RED labs on the use of the Watson Developer Cloud services
208
star
14

botkit-middleware

A middleware to connect Watson Conversation Service to different chat channels using Botkit
TypeScript
208
star
15

natural-language-classifier-nodejs

Deprecated: this demo will receive no further updates
JavaScript
158
star
16

dotnet-standard-sdk

🆕🆕🆕.NET Standard library to access Watson Services.
C#
146
star
17

android-sdk

🔆 Android SDK to use the IBM Watson services.
Java
145
star
18

assistant-with-discovery

DEPRECATED: this application is deprecated and thus will not receive fixes or security updates. It is archived for educational purposes, but may not function.
Java
145
star
19

natural-language-understanding-nodejs

🆕 Demo code for the Natural Language Understanding Service.
JavaScript
133
star
20

assistant-toolkit

Toolkit for experimentation with watsonx Assistant
HTML
100
star
21

car-dashboard

Application that demonstrates how the Watson Assistant service uses intent capabilities in an animated car dashboard UI.
JavaScript
91
star
22

node-red-node-watson

A collection of nodes for the IBM Watson services
HTML
83
star
23

discovery-nodejs

This is a deprecated Watson Discovery Service Demo. A link to the newly supported demo is below
JavaScript
76
star
24

go-sdk

🐭 go SDK for the IBM Watson services.
Go
71
star
25

assistant-improve-recommendations-notebook

Assistant Improve notebooks for Watson Assistant
Jupyter Notebook
68
star
26

investment-advisor

DEPRECATED: this repo is no longer actively maintained
JavaScript
66
star
27

speech-android-sdk

DEPRECATED - Please use https://github.com/watson-developer-cloud/android-sdk
Java
66
star
28

dialog-tool

DEPRECATED: this repo is no longer actively maintained
CSS
60
star
29

doc-tutorial-downloads

The download files from the IBM Watson Service documentation tutorials
Shell
55
star
30

language-translator-nodejs

Sample Node.js Application for the IBM Language Translator Service
CSS
51
star
31

sentiment-and-emotion

DEPRECATED: this repo is no longer actively maintained
CSS
50
star
32

simple-chat-swift

DEPRECATED: this repo is no longer actively maintained
Swift
48
star
33

ruby-sdk

♦️ Ruby SDK to use the IBM Watson services.
Ruby
45
star
34

raspberry-pi-speech-to-text

DEPRECATED: this repo is no longer actively maintained
JavaScript
44
star
35

food-coach

DEPRECATED: this repo is no longer actively maintained
JavaScript
39
star
36

assistant-skill-analysis

Dialog Skill Analysis framework for Watson Assistant
Jupyter Notebook
39
star
37

community

Example data that can be used for various Watson services
Shell
34
star
38

speech-to-text-swift

Speech-to-Text example using the Swift SDK
Swift
34
star
39

visual-recognition-code-pattern

JavaScript
33
star
40

react-components

DEPRECATED: this repo is no longer actively maintained
JavaScript
31
star
41

assistant-dialog-flow-analysis

Dialog Flow Analysis Notebook for Watson Assistant
HTML
28
star
42

salesforce-sdk

A Salesforce library for communicating with the IBM Watson REST APIs
Apex
28
star
43

conversation-connector

The Conversation connector is a set of components that mediate communication between your Conversation workspace and a Slack or Facebook app. Use the connector to deploy a chat bot that Slack or Facebook Messenger users can interact with.
JavaScript
27
star
44

document-conversion-nodejs

DEPRECATED: Please use https://github.com/watson-developer-cloud/discovery-nodejs
JavaScript
27
star
45

text-to-speech-java

DEPRECATED: this repo is no longer actively maintained
CSS
27
star
46

assistant-web-chat-service-desk-starter

A starter kit for building custom service desk integrations for Watson Assistant web chat
TypeScript
25
star
47

raspberry-pi-time-weather-demo

DEPRECATED: this repo is no longer actively maintained
JavaScript
24
star
48

assistant-demo

Assistant demo
JavaScript
23
star
49

discovery-starter-kit

IBM Watson Discovery Starter Kit
JavaScript
22
star
50

assistant-intermediate

An intermediate example of Watson Assistant in a Node.js application
JavaScript
22
star
51

discovery-components

IBM Watson Discovery components
TypeScript
22
star
52

assistant-with-discovery-openwhisk

DEPRECATED: this repo is no longer actively maintained
CSS
21
star
53

company-insights

DEPRECATED: this repo is no longer actively maintained
JavaScript
20
star
54

text-to-speech-swift

DEPRECATED: this repo is no longer actively maintained
Swift
20
star
55

speech-to-text-websockets-ruby

Ruby client that interacts with the IBM Watson Speech to Text service through its WebSockets interface
Ruby
19
star
56

social-customer-care

DEPRECATED: this repo is no longer actively maintained
CSS
19
star
57

customer-engagement-bot

DEPRECATED: this repo is no longer actively maintained
JavaScript
18
star
58

abap-sdk-nwas

ABAP code for using IBM Watson Developer Services with SAP NetWeaver Application Server, imported via abapGit
ABAP
18
star
59

assistant-web-chat

Language strings for web chat integration of IBM watsonx assistant
JavaScript
16
star
60

python-primer-companion-code

DEPRECATED: this repo is no longer actively maintained
Python
15
star
61

spring-boot-starter

Spring Boot support for Watson services
Java
13
star
62

personality-insights-java

DEPRECATED: this repo is no longer actively maintained
CSS
13
star
63

watson-developer-cloud.github.io

Index page with links to SDKs, docs, etc.
HTML
13
star
64

simple-chat-objective-c

DEPRECATED: this repo is no longer actively maintained
Objective-C
12
star
65

ui-components

DEPRECATED: this repo is no longer actively maintained
CSS
12
star
66

openwhisk-sdk

🆕 SDK for using Watson Services on IBM Cloud Functions (based on Apache Openwhisk) - DEPRECATED
JavaScript
12
star
67

text-bot-openwhisk

DEPRECATED: this repo is no longer actively maintained
JavaScript
12
star
68

app-insights-discovery

DEPRECATED: this repo is no longer actively maintained
Swift
10
star
69

customer-engagement-nodejs

Customer Engagement
JavaScript
10
star
70

token-generator

Basic Node.js Server to generate watson auth tokens from user-supplied credentials.
JavaScript
6
star
71

watson-vision-coreml-code-pattern

Watson Visual Recognition CoreML Code Pattern
CSS
5
star
72

abap-sdk-scp

ABAP code for using IBM Watson Developer Services with SAP Cloud Platform, imported via abapGit with dependencies via APACK
ABAP
5
star
73

restkit

Core networking and authentication library for the Watson Swift SDK
Swift
4
star
74

cognitive-client-java

DEPRECATED: this repo is no longer actively maintained
Java
4
star
75

speech-to-text-utils

Speech to text CLI that helps you manage speech customizations.
JavaScript
4
star
76

homebrew-tools

DEPRECATED: this repo is no longer actively maintained
Ruby
3
star
77

language-translator-tooling

DEPRECATED: this repo is no longer actively maintained
JavaScript
2
star
78

discovery-nodejs-static

Sample Node.js application that uses the IBM Watson Discovery Service
JavaScript
2
star
79

natural-language-classifier-intent-classification-demo

Deprecated
JavaScript
2
star
80

natural-language-understanding-code-pattern

Natural Language Understanding Code Pattern
JavaScript
2
star
81

speech-to-text-code-pattern

React app using the Watson Speech to Text service to transform voice audio into written text.
JavaScript
2
star
82

swift-playgrounds

Swift playgrounds for Watson Developer Cloud services
Swift
2
star
83

actions-logging-server

HTML
1
star
84

sdk-example-editor

Web application that helps edit SDK examples from an OpenAPI file.
JavaScript
1
star
85

Watson-Assistant-Workspace-Retrain

Python
1
star
86

actions-analytics-dashboard

JavaScript
1
star
87

assistant-web-chat-react

A React library to make integration of Watson Assistant web chat with a React application easy.
TypeScript
1
star
88

visual-recognition-utils

Command line tools to make creating & managing Watson Visual Recognition Custom Classifiers easier.
JavaScript
1
star