Blog

How to Use Console API to Create a Data Table

by on May 2, 2019

Create Data Table via Console API

If you’ve worked with Backendless API for a while, you may occasionally run into a situation where the functionality you’d like to have isn’t readily available. One such function is the programmatic management of your application’s data tables. For instance, you may need to clear up all the data and recreate the table structure with specific columns on demand while developing your app. This article will show you how to do that and more.

The Backendless Console API

Backendless essentially has two APIs: the Client API and the Console API. The former is the one used by Backendless’ Client SDKs and is in effect in our REST SDK. The latter is the API that the Developers’ Console is using, hence the name. Different providers may have different names for that one, such as Admin API or Management API, so be aware that the terms may sometimes be used interchangeably.

Creating a table

As you may have already supposed, the Console API is also a kind of REST API accessible by any HTTP client. In order to perform a request against it and receive a proper response, you need to know the following things:

  • request URL and method type,
  • auth-key header value, and
  • request body.

The auth-key header in the Console API is similar to the user-token in the Client SDKs, with the exception that you can’t avoid it: unauthorized requests are not allowed in Console API (because you cannot use the Console without logging in as a developer). And while the user-token‘s owner is your app’s user, the auth-key determines the developer currently using the console.

To find out the mentioned elements, you have to inspect the requests issued by Backendless Console using the browser’s developer console. For Chrome, use either the F12 button or Ctrl+Shift+I combination, or go to the Settings menu -> More tools -> Developer tools.

To see the request for the data table creation, switch to the Network tab in a browser’s console and create a table using Backendless Console. Here’s an example of what you’ll see as a result:

Request URL, method and auth-key header

Request Content-Type header and body

Having the required values, you can reproduce the request using an HTTP client of your preference. Here’s an example for Java/Android (do not forget to substitute YOUR-APP-ID and your-auth-key values with your own):

final URL url = new URL( "http://develop.backendless.com/YOUR-APP-ID/console/data/tables" );
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try
{
  urlConnection.setRequestMethod( "POST" );
  urlConnection.setRequestProperty( "Content-Type", "application/json" );
  urlConnection.setRequestProperty( "auth-key", "your-auth-key" );
  urlConnection.setDoOutput( true );

  OutputStream out = new BufferedOutputStream( urlConnection.getOutputStream() );
  out.write( "{\"name\":\"TestTable\"}".getBytes( StandardCharsets.UTF_8 ) );
  out.close();

  final InputStream err = urlConnection.getErrorStream();
  if( err != null )
  {
    final String fault = new Scanner( err ).useDelimiter( "\\A" ).next();
    System.err.println( fault );
    return;
  }

  final InputStream in = urlConnection.getInputStream();
  final String result = new Scanner( in ).useDelimiter( "\\A" ).next();
  System.out.println( result );
}
finally
{
  urlConnection.disconnect();
}

As a result of a successful request, you should see the response JSON with table ID and columns’ descriptions (example shortened for brevity):

{
  "tableId": "22E546E8-04F3-8B2E-FFA1-9517E93F5100",
  "name": "Person1",
  "columns": [
    {
      "columnId": "7B3DCBB8-7BD7-F85E-FF23-C2E1A798B600",
      "name": "created",
      ...
    },
    {
      "columnId": "77675BA3-52C7-E381-FFAE-58A2CDF39700",
      "name": "updated",
      ...
    },
    {
      "columnId": "CEA75DB7-C3D5-6F30-FF28-E5B3BCDE4A00",
      "name": "objectId",
      ...
    }
  ],
  ...
}

Perform other console operations

You can use the same steps to find out the parameters for almost any Console request. For example, here is the request to delete a table (this required no body, but still needs auth-key header specified):

Request URL: http://develop.backendless.com/YOUR-APP-ID/console/data/tables/YourTableName
Request method: DELETE

And here’s one to create a column with specified parameters:

Request URL: http://develop.backendless.com/YOUR-APP-ID/console/data/tables/YourTableName/columns
Request method: POST
Request body:
{
 "dataType":"STRING",
 "name":"newColumn",
 "required":false,
 "unique":false,
 "indexed":false,
 "defaultValue":null,
 "customRegex":null
}

The Console API caveats

The Console API was originally meant to be internal and not used in any automation. That’s why it was never added to the Client SDKs. This wasn’t a technical decision, but rather a measure to increase the stability of the application’s work. The operations available only with Console API are mostly the ones changing the database structure or some internal configurations. All these modifications should be done prior to releasing your application and should not be changed after that, since it may break your app. Moreover, these operations are usually synchronized on the whole app, so they tend to be slow, which also negatively reflects on your end-user’s experience.

This post should be considered as a description of a workaround, not an actual guide on how to write your applications. While development these functionalities may be very convenient, we strongly discourage using this API in your production app code. It is also subject to occasional changes for our convenience, and we do not notify our customers about those changes.

Thanks for reading, and happy coding!

Leave a Reply