Learn to Code via Tutorials on Repl.it

← Back to all posts
repl-talk.js in NodeJS
LeonDoesCode (271)

Another One?

So, you may remember this tutorial I done: Replit.js in NodeJS. Well, this is different. That project has had its name switched and I can no longer seem to get the script to work in its current state. So, I made my own API for all of you.

Note: It may be a bit basic at the moment, but I'll keep on adding to it as time goes on.

You can find the npm package here.

How it Works

I think the best thing for me to do, is explain how it works so that you know what you're getting yourself into.

...Client --> Login --> Query --> Request...

Client:
This is how you have access to the API.

Login:
You have to login to use the API, it:

  • Shows that you have an account with https://repl.it
  • We can send your ID with your requests

Query:
You will then be able to query data and get what you need.

Request:
After querying, you will send off a request which returns the data you will be using.

It's a very simple process, but makes sure that you and the https://repl.it team have the best use out of this API.

Getting Started

After we install repl-talk.js, we can start to use it. First, let's require it and create the client that we will use to access the API:

const replit = require("repl-talk.js");
const client = new replit.Client();

Now we have our client, we can use login(username, password, callback) to "login" to the API. While we aren't logging into the API services, we are making sure that you have a valid https://repl.it account. Without an account, you will not be able to use the API. So let's login:

client.login("DummyAccount01", process.env.password, () => {
    // Next lot of code will go here
});

I feel that it is important to say a few things. The account username and password, along with any other data you may use is not recorded in anyway by the API. However, your account's id is send with the request header, so that https://repl.it knows who has sent the request. We do this to ensure that any API abuse can be caught and handled by the professionals at https://repl.it. Apart from this, your account is only used to verify that you are registered with https://repl.it.

This is the basic code for the API which will get you logged in and ready to go.

All the Queries!

Here is every single query that comes with the API thus far:

  // Gets data on a user using their id
  client.users.getUserById(1, body => {
    console.log(body);
  });

  // Gets data on an user using their username
  client.users.getUserByUsername("LeonDoesCode", body => {
    console.log(body);
  });

  // // Gets data on a repl using its id
  client.users.getReplById("47d50bc9-00e0-47d0-81f3-7195fa83a3e2", body => {
    console.log(body);
  });

  // // Gets data on an enterprize using its id
  client.users.getEnterprizeById(1, body => {
    console.log(body);
  });

  // // Gets data on an organization using its id
  client.users.getOrganizationById(15, body => {
    console.log(body);
  });

  // // Gets data on a post using its id
  client.posts.getPostById(22904, body => {
    console.log(body);
  });

  // // Gets data on a board using its id
  client.posts.getBoardById(client.posts.SHARE, body => {
    console.log(body);
  });

  // // Gets data on a comment using its id
  client.posts.getCommentById(68493, body => {
    console.log(body);
  });

If you run each of these individually, you can easier see what you're dealing with. The data is in a nice JSON formal, and easily usable. These are pretty self explanitory, but let me quote myself:

Let's cover some of the user based requests that you may use. Because these are user requests, when we want to use them we have to access them through client.users. This helps to split up the code between user and post requests, so that is is easier to see what is happening.

The callbacks for every function will return body. body contains the data for the query.

Same goes for the posts too.

Conclusion

This was not my normal layout for a tutorial, sorry if it seemed much more chaotic and messy than the others. However, I hope that you now have an easy way of using repl in your projects none the less.

P.S
If you have any suggestions for tutorials, leave them in the comments and I'll be sure to have a look. If you like one in the comments, then give it an up vote to show that you want to see it. It makes my life so much more easier. Thanks in advance!