Examples on Connecting to Web Services

You can easily connect to external REST APIs using DreamFactory. This three minute screencast provides a quick primer.

First, in the “Services” tab of the Admin Console just select “Remote Web Service” and specify any headers and parameters required to access the web service. There’s a blog post here with an example of connecting to the Edmunds API.

That’s it, you can start calling those services. You can think of DreamFactory as a tree of REST services…/rest/serviceFoo/…/… and /rest/serviceFooBar/…/…, etc. for all your services. For remote services, each service works “as if” you were calling it directly, but we add in the extra headers and URL parameters, which are hidden securely on the server.

There’s an optional step if you want to document your remote APIs with Swagger and consume them with any of the DreamFactory client SDKs. You need to upload a simple JSON file to the DreamFactory file system. DreamFactory uses Swagger for REST API definitions. This JSON file defines the web service that you are accessing with DreamFactory as the secure proxy. More on the Swagger REST specification is here. Note that a future release of DreamFactory will make defining your APIs easier with a Swagger configuration UI (RAML support is on the way too).

Once you’ve uploaded your Swagger JSON file, you can test your API calls to the remote web service from the “API Docs” tab in the DreamFactory Admin Console.

This blog post shows an example of configuring the Rotten Tomatoes API with DreamFactory. The JSON file that defines the Rotten Tomatoes service looks like this:

{
   "swaggerVersion":"1.2",
   "apiVersion":"1.0",
   "basePath":"https://dsp-foo.cloud.dreamfactory.com/rest",
   "resourcePath":"/movies",
   "produces":[
      "application/json"
   ],
   "consumes":[
      "application/json"
   ],
   "apis":[
      {
         "path":"/movies",
         "operations":[
            {
               "method":"GET",
               "summary":"Get a recordset of movies based on a query parameter",
               "nickname":"getMovies",
               "type":"Movies",
               "parameters":[
                  {
                     "name":"q",
                     "description":"Movie name as a string",
                     "allowMultiple":false,
                     "type":"string",
                     "paramType":"query",
                     "required":true
                  }
               ],
               "responseMessages":[
                  {
                     "message":"Bad Request - Request does not have a valid format, all required parameters, etc.",
                     "code":400
                  },
                  {
                     "message":"System Error - Specific reason is included in the error message.",
                     "code":500
                  }
               ],
               "notes":"Use a plus sign between words in titles with multiple words"
            }
         ],
         "description":"Operations for resource type administration."
      }
   ],
   "models":{
      "Movies":{
         "id":"Movies",
         "properties":{
            "resource":{
               "type":"Array",
               "items":{
                  "$ref":"Movie"
               }
            }
         }
      },
      "Movie":{
         "id":"Movie",
         "properties":{
            "name":{
               "type":"string"
            }
         }
      },
      "Success":{
         "id":"Success",
         "properties":{
            "success":{
               "type":"boolean"
            }
         }
      }
   }
}
1 Like

Now in the 1.7.6 release you no longer have to upload the JSON file, just copy it directly into the Service Definition.