Glitch API SDK on TypeScript - glitch-api

Here is the original post for version 1

glitch-api

GitHub package.json dynamic

CircleCI

npm bundle size

A Node.js module that allows you to easily interact with the Glitch API

| :open_book: Documentation | :sparkles: Examples |

| ----------------------------------------------------------------------------- | --------------------------------------------------------------------------------- |

Features

  • 99% coverage of the known Glitch API

  • Uses TypeScript that provides hints in editor, type checking, etc.

  • Supports WebSocket connection to the Glitch editor

  • Support for authorization

  • Only two dependencies: node-fetch and algoliasearch

  • Class abstraction

  • Works with both API versions

Warning: this module uses unstable API that hasn’t been officially released yet. Described only world-open methods from this unofficial site and my researches

Installation

Node.js 8.0.0 or newer is required

NPM

npm i glitch-api -s

Example usage


// Require using ES6 syntax

import { Glitch } from 'glitch-api'

// Or using old fancy style

const { Glitch } = require('glitch-api')

// Init main class

const glitch = new Glitch({ token: 'xxx' })  // Put here your Glitch token or use glitch.setAnonToken()

const { api } = glitch

// Get a user profile

api.users.get({ id: 1 }).then(user => console.log) // → User

:exclamation: Migration from 2.x to 3.x

  • Any search method now follows the same syntax:

    
    .search(s: string) → Algolia result
    
    
  • Remixing a project doesn’t return joinLink. Now it is a Project instance. Consider getting project data with token via api.projects.get()

  • Token is absolutely required. If you don’t have a token, consider using Glitch.setAnonToken(). For more info, see examples/anonymous.js

Q&A

How do I get Glitch token?

Paste the following code to the browser’s console on the Glitch editor page:


(JSON.parse(localStorage.getItem('cachedUser'))).persistentToken

Also you can use Glitch.setAnonToken() → Promise<string> method to login as an anonymous user and use its token.


Why *api-method* is not implemented?

Because I’m also human and I might not have seen the recent changes in Glitch’s API.

Anyway, PRs are open for anyone :slight_smile:


The *api-method* is not working.

It could be that Glitch devs removed the support for that method. Or it’s just a my fault.

Open a new issue and describe what’s happend.

Contribution

Feel free to open new Pull request or an issue!

Credits

Made by jarvis394 with :heart:

9 Likes

@jarvis394, how do I get a user ID?

1 Like

This module seems great! Now I can automate remixing projects so I don’t have to manually remix them which makes this module very useful for me!

I think you might be able to get it from ‘Network’ panel in your browser’s dev panel. In Chrome, you can access it by pressing F12 and selecting ‘Network’. There you can see all requests and responses, so go to your profile page and see, what ID it is requesting.

Also, you can get a project by its domain and then see IDs of project members: https://glapi.glitch.me/classes/member#id

1 Like

Actually, the example I have in GitHub repo (/docs/examples/remixProject.js) is wrong, change that to this:

- console.log(remix.inviteLink)
+ console.log(remix.joinLink)
2 Likes

I couldn’t find anything in the Network panel. All I could see were all the assets loading and some project ids. I’ll try the other way!

1 Like

is there any other way to get a user token?

Yes, I got the ID:

api.projects.get({ domain: '<CUSTOM DOMAIN>' }).then(project => console.log(project))

will give you a result:

Project {
  id: 'xxx-slss-some-numbers',
  description: 'An online tool for easy manual transcribing of audios and videos. ',
  domain: 'typeaudio',
  baseId: 'some-numbers',
  private: undefined,
  likesCount: 0,
  isSuspended: false,
  suspendedReason: '',
  avatarUpdatedAt: 2019-12-05T14:38:35.472Z,
  showAsGlitchTeam: false,
  isEmbedOnly: false,
  remixChain: [ 'some-numbers-xxx-xx-xx' ],
  notSafeForKids: false,
  createdAt: 2019-10-16T23:44:06.682Z,
  updatedAt: 2019-12-16T10:07:09.351Z,
  permissions: [ Member { id: <YOU WILL FIND THE ID HERE>, accessLevel: 30 } ],
  features: [],
  teamIds: [] }

thx,

i will test this out immediately :smiley:

@jarvis394,

This is not working:

api.users.get({ id: 3XXXXX }).then(user => console.log) // → User

EDIT: It’s fixed! @jarvis394, please correct the statement as:

api.users.get({ id: 1 }).then(user => console.log(user))

wait, i need a token to get to that, but i cant retrieve my token because i have to use dev tools which are disabled for me.

@J-Tech-Foundation,

Do this:

Type in any of your custom domain and the token appears in the Glitch Logs, not the Dev Tools!

const glitch = new Glitch({ token: 'xxx' })  // Put here your Glitch token
const { api } = glitch //          ^^^^

i need the token

Oh! Why not enable Dev Tools?

cant, its disabled on my chromebook by my school

1 Like

@jarvis394, is there any way to edit a project using the API?

Paste this in your searchbar (javascript: is automatically removed, so you have to add it before the script):

javascript:(JSON.parse(localStorage.getItem('cachedUser'))).persistentToken

Yep, sorry, tried to minify the example :roll_eyes:

Yes, I was searching for the exact parameters in the editor’s obfuscated code, but I’m still working on it. Surely, it would be the greatest feature of this package! I wonder how many things are possible to do with it.

Actually, all you have to do is connect by WebSockets to your application editor. Sounds pretty easy, but reengineering the code is pretty hard (for me).

If you have any thoughts about the Editor API, share it here

Hello there!

I’m actually might get to start WebSockets stuff going, so I hope in next release I would add:
Support for collections
Support for team pins
Class for remixed project and an interface to it
And, most important, Glitch’s editor abilities, like application.files() or application.logsData()

image

See you soon!

2 Likes