Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

akhil avatar image
akhil asked Erick Ramirez commented

Getting "Request failed with status code 401" creating a document on Astra DB

I am trying to create a collection using the astrajs/collections. I am also using the netlify cli to test my application. So I proceeded to make a small post to check if my database is working. Using the following code. The expected behavior is to create a post like this:

- a post: {
  
  title: "My first post"
}

I did check my credentials inside my .env file it seems fine. This was the full error

Request from ::ffff:127.0.0.1: GET /.netlify/functions/addData
Error: Request Failed: [object Object]
Stack Trace: Request failed with status code 401
at axiosRequest (D:\tiktokclone\node_modules\@astrajs\rest\src\rest.js:126:11)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async AstraClient._request (D:\tiktokclone\node_modules\@astrajs\rest\src\rest.js:199:22)
at async AstraClient.put (D:\tiktokclone\node_modules\@astrajs\rest\src\rest.js:263:12)
at async AstraCollection._put (D:\tiktokclone\node_modules\@astrajs\collections\src\collections.js:69:22)
at async Object.exports.handler (D:\tiktokclone\functions\addData.js:17:5)

Response with status 500 in 742 ms.

 const astraClient = await createClient({
    astraDatabaseId: process.env.ASTRA_DB_ID,
    astraDatabaseRegion: process.env.ASTRA_DB_REGION,
    applicationToken: process.env.ASTRA_DB_APPLICATION_TOKEN,
  });

  const posts = astraClient
    .namespace(process.env.ASTRA_DB_KEYSPACE)
    .collection(collection);

  try {
    await posts.create("a post", {
      title: "my first post",
    });

    return {
      statusCode: 200,
    };
  } catch (e) {
    console.error(e);
    return {
      statusCode: 500,
      body: JSON.stringify(e),
    };
  }
astra db
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered Erick Ramirez commented

Your code looks unfamiliar to me and seems incomplete.

It would be ideal if you could post a link to the document you're following because it's not clear to me whether you're connecting to the Stargate REST API or Document API.

I suspect based on the 401 error that the issue relates to you not specifying the client ID and secret as in this example:

  const astraClient = await createClient({
      astraDatabaseId: process.env.ASTRA_DB_ID,
      astraDatabaseRegion: process.env.ASTRA_DB_REGION,
      username: process.env.ASTRA_DB_USERNAME,
      password: process.env.ASTRA_DB_PASSWORD,
  });

Note that the username is your client ID and the password is your client secret.

Please try using the example for Node.js REST client and let me know how you go. I'm really interested in getting the link to the document you're following so we can get in contact with the author to make any necessary updates so users like you don't run into this problem. Cheers!

5 comments Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Hey, it works when I did it this way. Yes, it was for a REST API and I was using this documentation:https://docs.datastax.com/en/astra/docs/astra-collection-client.html....

There instead of username and password it uses the application token which kept giving me the 401 error.

0 Likes 0 ·
I see now where the confusion is. The REST API uses your client ID + secret to authenticate whereas the Doc API uses your app token.

The link you posted is for the Doc API but you're querying with the REST API. Cheers!

0 Likes 0 ·

Thank you so much. Cheers

0 Likes 0 ·
I wanted to put a beer emoji but I couldn't. :(
0 Likes 0 ·

LOL. Yeah, the editor doesn't support emojis. :)

0 Likes 0 ·