Stargate Api Rest - datastaxdevs/awesome-astra GitHub Wiki

Overview

  • Rational
  • Stargate reference documentations
  • way to use it in standalone way

Prerequisite

  • Astra instance
  • Token creation

How to

  • Swagger access
  • Walkthrough 10 operations through swagger

4. Use REST API

This walkthrough has been realized using the REST API Quick Start. Here we will the the DATA or SwaggerUI

image

✅ a. List keyspaces

  • GET: /v2/schemas/keyspaces

image

  • Click Try it out
  • Provide your token in the field X-Cassandra-Token
  • Click on Execute

✅ b. List Tables

  • GET /v2/schemas/keyspaces/{keyspaceName}/tables

image

  • Click Try it out
  • Provide your token in the field X-Cassandra-Token
  • keyspace: ks1
  • Click on Execute

✅ c. List Types

  • GET /v2/schemas/keyspaces/{keyspaceName}/types

image

  • Click Try it out
  • X-Cassandra-Token: <your_token>
  • keyspace: ks1
  • Click on Execute

✅ d. Create a Table

  • POST /v2/schemas/keyspaces/{keyspaceName}/tables

image

  • Click Try it out
  • X-Cassandra-Token: <your_token>
  • keyspace: ks1
  • Data
{
  "name": "users",
  "columnDefinitions":
    [
        {
        "name": "firstname",
        "typeDefinition": "text"
      },
        {
        "name": "lastname",
        "typeDefinition": "text"
      },
      {
        "name": "email",
        "typeDefinition": "text"
      },
        {
        "name": "color",
        "typeDefinition": "text"
      }
    ],
  "primaryKey":
    {
      "partitionKey": ["firstname"],
      "clusteringKey": ["lastname"]
    },
  "tableOptions":
    {
      "defaultTimeToLive": 0,
      "clusteringExpression":
        [{ "column": "lastname", "order": "ASC" }]
    }
}

👁️ Expected output

{
  "name": "users"
}

✅ e. Insert Rows

Notice than for the DML you move to DATA. Make sure you are using url with V2, V1 would also work but this is NOT the same payload.

  • POST /v2/keyspaces/{keyspaceName}/{tableName}

image

  • X-Cassandra-Token: <your_token>
  • keyspaceName: ks1
  • tableName: users
  • Data
{   
    "firstname": "Cedrick",
    "lastname": "Lunven",
    "email": "[email protected]",
    "color": "blue"
}

You can note that the output code is 201 and return your primary key `{ "firstname": "Cedrick","lastname": "Lunven" }

  • You can add a second record changing only the payload
{
    "firstname": "David",
    "lastname": "Gilardi",
    "email": "[email protected]",
    "color": "blue"
}
  • Add a third
{
    "firstname": "Kirsten",
    "lastname": "Hunter",
    "email": "[email protected]",
    "color": "pink"
}

✅ f. Read multiple rows

  • GET /v2/keyspaces/{keyspaceName}/{tableName}/rows image

  • X-Cassandra-Token: <your_token>

  • keyspaceName: ks1

  • tableName: users

  • Click Execute

  • Notice how now you can only limited return fields

  • fields: firstname, lastname

✅ g. Read a single partition

  • GET /v2/keyspaces/{keyspaceName}/{tableName}/{primaryKey}

image

  • X-Cassandra-Token: <your_token>
  • keyspaceName: ks1
  • tableName: users
  • primaryKey; 'Cedrick`
  • Click Execute
- Important: The Swagger user interface is limited as of now and you cannot test a composite key (here adding Lunven). This is a bug in the UI not the API.

✅ h. Delete a row

  • DELETE /v2/keyspaces/{keyspaceName}/{tableName}/{primaryKey}

image

  • X-Cassandra-Token: <your_token>
  • keyspaceName: ks1
  • tableName: users
  • primaryKey; 'Cedrick`
  • Click Execute

✅ i. Searches

  • GET /v2/keyspaces/{keyspaceName}/{tableName}

image

  • X-Cassandra-Token: <your_token>
  • keyspaceName: ks1
  • tableName: users
  • whereClause; '{"firstname": {"$eq":"David"}}`
  • Click Execute

I let you try with {"lastname": {"$eq":"Gilardi"}}.. expected right ?

Extra Resource

  • Provide the Postman collections
  • What's next (using HTTP Clients or SDK)
⚠️ **GitHub.com Fallback** ⚠️