Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39537
1
###############################################################################
2
#
3
# CoCalc: Collaborative Calculation in the Cloud
4
#
5
# Copyright (C) 2016, Sagemath Inc.
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#
20
###############################################################################
21
22
###
23
#
24
# Library for working with JSON messages for Salvus.
25
#
26
# We use functions to work with messages to ensure some level of
27
# consistency, defaults, and avoid errors from typos, etc.
28
#
29
###
30
31
doc_intro = """
32
## About the API
33
34
### Purpose
35
36
The purpose of the CoCalc API (application programming interface) is to make
37
essential operations within the CoCalc platform available to automated
38
clients. This allows embedding of CoCalc services within other products
39
and customizing the external look and feel of the application.
40
41
### Protocol and Data Format
42
43
Each API command is invoked using an HTTPS PUT request.
44
All commands support request parameters in JSON format, with request header
45
`Content-Type: application/json`. Many commands (those that do not
46
require lists or objects as parameters)
47
also accept request parameters as key-value pairs, i.e.
48
`Content-Type: application/x-www-form-urlencoded`.
49
50
Responses are formatted as JSON strings.
51
Note that it is possible for a request to fail and return
52
a response code of 200. In that case, the response
53
string may contain helpful information on the nature of
54
the failure. In other cases, if the request cannnot
55
be completed, a response code other than 200 may be
56
returned, and the response body may be a
57
generic HTML message rather than a JSON string.
58
59
### Authentication
60
61
A valid API key is required on all API requests.
62
To obtain a key, log into
63
CoCalc and click on Settings (gear icon next to user name at upper
64
right), and look under `Account Settings`.
65
With the `API key` dialogue, you can create a key,
66
view a previously assigned key, generate a replacement key,
67
and delete your key entirely.
68
69
Your API key carries access privileges, just like your
70
login and password.
71
__Keep it secret.__
72
Do not share your API key with others or post it in publicly accessible
73
forums.
74
75
### Additional References
76
77
- The CoCalc PostgreSQL schema definition
78
[src/smc-util/db-schema.coffee](https://github.com/sagemathinc/cocalc/blob/master/src/smc-util/db-schema.coffee)
79
has information on tables and fields used with the API `query` request.
80
- The API test suite
81
[src/smc-hub/test/api/](https://github.com/sagemathinc/cocalc/tree/master/src/smc-hub/test/api)
82
contains mocha unit tests for the API messages.
83
- The CoCalc message definition file
84
[src/smc-util/message.coffee](https://github.com/sagemathinc/cocalc/blob/master/src/smc-util/message.coffee)
85
contains the source for this guide.
86
87
### API Message Reference
88
89
The remainder of this guide explains the individual API endpoints.
90
Each API request definition begins with the path of the
91
URL used to invoke the request,
92
for example `/api/v1/change_email_address`.
93
The path name ends with the name of the request,
94
for example, `change_email_address`.
95
Following the path is the list of options.
96
After options are one or more sample invocations
97
illustrating format of the request as made with the `curl`
98
command, and the format of the response.
99
100
The following two options appear on all API messages
101
(request parameters are often referred to
102
as 'options' in the guide):
103
104
- **event**: the command to be executed, for example "ping"
105
- **id**: uuid for the API call, returned in response in most cases.
106
If id is not provided in the API message, a random id will be
107
generated and returned in the response.
108
"""
109
110
misc = require('./misc')
111
defaults = misc.defaults
112
required = defaults.required
113
_ = require('underscore')
114
115
116
message = (obj) ->
117
exports[obj.event] = (opts={}, strict=false) ->
118
if opts.event?
119
throw Error("ValueError: must not define 'event' when calling message creation function (opts=#{JSON.stringify(opts)}, obj=#{JSON.stringify(obj)})")
120
defaults(opts, obj, false, strict)
121
return obj
122
123
# message2 for "version 2" of the message definitions
124
# TODO document it, for now just search for "message2" to see examples
125
message2 = (obj) ->
126
127
mk_desc = (val) ->
128
desc = val.desc
129
if val.init == required
130
desc += ' (required)'
131
else if val.init?
132
desc += " (default: #{misc.to_json(val.init)})"
133
return desc
134
135
# reassembling a version 1 message from a version 2 message
136
mesg_v1 = _.mapObject(obj.fields, ((val) -> val.init))
137
mesg_v1.event = obj.event
138
# extracting description for the documentation
139
fdesc = _.mapObject(obj.fields, mk_desc)
140
exports.documentation.events[obj.event] =
141
description : obj.desc ? ''
142
fields : fdesc
143
# ... and the examples
144
exports.examples[obj.event] = obj.examples
145
# wrapped version 1 message
146
message(mesg_v1)
147
return obj
148
149
# messages that can be used by the HTTP api. {'event':true, ...}
150
exports.api_messages = {}
151
152
# this holds the documentation for the message protocol
153
exports.documentation =
154
intro : doc_intro
155
events : {}
156
157
# holds all the examples: list of expected in/out objects for each message
158
exports.examples = {}
159
160
API = (obj) ->
161
# obj could be message version 1 or 2!
162
exports.api_messages[obj.event] = true
163
164
############################################
165
# Compute server messages
166
#############################################
167
168
message
169
event : 'compute_server_status'
170
status : undefined
171
172
# Message for actions using a compute server
173
message
174
event : 'compute'
175
project_id : undefined
176
action : required # open, save, ...
177
args : undefined
178
param : undefined # deprecate
179
id : undefined
180
181
message
182
event : 'project_state_update'
183
project_id : required
184
state : required
185
time : required
186
state_error : undefined # error if there was one transitioning to this state
187
188
189
############################################
190
# Sage session management; executing code
191
#############################################
192
193
# hub --> sage_server&console_server, etc. and browser --> hub
194
message
195
event : 'start_session'
196
type : required # "sage", "console"; later this could be "R", "octave", etc.
197
# TODO: project_id should be required
198
project_id : undefined # the project that this session will start in
199
session_uuid : undefined # set by the hub -- client setting this will be ignored.
200
params : undefined # extra parameters that control the type of session
201
id : undefined
202
limits : undefined
203
204
# hub --> browser
205
message
206
event : 'session_started'
207
id : undefined
208
session_uuid : undefined
209
limits : undefined
210
data_channel : undefined # The data_channel is a single UTF-16
211
# character; this is used for
212
# efficiently sending and receiving
213
# non-JSON data (except channel
214
# '\u0000', which is JSON).
215
216
# hub --> client
217
message
218
event : 'session_reconnect'
219
session_uuid : undefined # at least one of session_uuid or data_channel must be defined
220
data_channel : undefined
221
222
223
#
224
225
# client --> hub
226
message
227
event : 'connect_to_session'
228
id : undefined
229
type : required
230
project_id : required
231
session_uuid : required
232
params : undefined # extra parameters that control the type of session -- if we have to create a new one
233
234
message
235
event : 'session_connected'
236
id : undefined
237
session_uuid : required
238
data_channel : undefined # used for certain types of sessions
239
history : undefined # used for console/terminal sessions
240
241
242
# sage_server&console_server --> hub
243
message
244
event : 'session_description'
245
pid : required
246
limits : undefined
247
248
# client <----> hub <--> sage_server
249
message
250
event : 'terminate_session'
251
project_id : undefined
252
session_uuid : undefined
253
reason : undefined
254
done : undefined
255
256
message
257
event : 'execute_code'
258
id : undefined
259
code : required
260
data : undefined
261
session_uuid : undefined
262
cell_id : undefined # optional extra useful information about which cells is being executed
263
preparse : true
264
allow_cache : true
265
266
# Output resulting from evaluating code that is displayed by the browser.
267
# sage_server --> local hub --> hubs --> clients
268
message
269
event : 'output'
270
id : undefined # the id for this particular computation
271
stdout : undefined # plain text stream
272
stderr : undefined # error text stream -- colored to indicate an error
273
html : undefined # arbitrary html stream
274
md : undefined # github flavored markdown
275
tex : undefined # tex/latex stream -- is an object {tex:..., display:...}
276
d3 : undefined # d3 data document, e.g,. {d3:{viewer:'graph', data:{...}}}
277
hide : undefined # 'input' or 'output'; hide display of given component of cell
278
show : undefined # 'input' or 'output'; show display of given component of cell
279
auto : undefined # true or false; sets whether or not cell auto-executess on process restart
280
javascript : undefined # javascript code evaluation stream (see also 'execute_javascript' to run code directly in browser that is not part of the output stream).
281
interact : undefined # create an interact layout defined by a JSON object
282
obj : undefined # used for passing any JSON-able object along as output; this is used, e.g., by interact.
283
file : undefined # used for passing a file -- is an object {filename:..., uuid:..., show:true}; the file is at https://cloud.sagemath.com/blobs/filename?uuid=[the uuid]
284
raw_input : undefined # used for getting blocking input from client -- {raw_input:{prompt:'input stuff?', value:'', submitted:false}}
285
done : false # the sequences of messages for a given code evaluation is done.
286
session_uuid : undefined # the uuid of the session that produced this output
287
once : undefined # if given, message is transient; it is not saved by the worksheet, etc.
288
clear : undefined # if true, clears all output of the current cell before rendering message.
289
events : undefined # {'event_name':'name of Python callable to call', ...} -- only for images right now
290
291
# This message tells the client to execute the given Javascript code
292
# in the browser. (For safety, the client may choose to ignore this
293
# message.) If coffeescript==true, then the code is assumed to be
294
# coffeescript and is first compiled to Javascript. This message is
295
# "out of band", i.e., not meant to be part of any particular output
296
# cell. That is why there is no id key.
297
298
# sage_server --> hub --> client
299
message
300
event : 'execute_javascript'
301
session_uuid : undefined # set by the hub, since sage_server doesn't (need to) know the session_uuid.
302
code : required
303
obj : undefined
304
coffeescript : false
305
cell_id : undefined # if set, eval scope contains an object cell that refers to the cell in the worksheet with this id.
306
307
308
############################################
309
# Information about several projects or accounts
310
#############################################
311
312
API message2
313
event : 'get_usernames'
314
fields:
315
id:
316
init : undefined
317
desc : 'A unique UUID for the query'
318
account_ids:
319
init : required
320
desc : 'list of account_ids'
321
desc : """
322
Get first and last names for a list of account ids.
323
324
Note: Options for the `get_usernames` API message must be sent as JSON object.
325
326
Example:
327
```
328
curl -u sk_abcdefQWERTY090900000000: -H "Content-Type: application/json" \\
329
-d '{"account_ids":["cc3cb7f1-14f6-4a18-a803-5034af8c0004","9b896055-920a-413c-9172-dfb4007a8e7f"]}' \\
330
https://cocalc.com/api/v1/get_usernames
331
==> {"event":"usernames",
332
"id":"32b485a8-f214-4fda-a622-4dbfe0db2b9c",
333
"usernames": {
334
"cc3cb7f1-14f6-4a18-a803-5034af8c0004":{"first_name":"John","last_name":"Smith"},
335
"9b896055-920a-413c-9172-dfb4007a8e7f":{"first_name":"Jane","last_name":"Doe"}}}
336
```
337
"""
338
339
message
340
event : 'usernames'
341
id : undefined
342
usernames : required
343
344
345
############################################
346
# Account Management
347
#############################################
348
349
# client --> hub
350
API message2
351
event : 'create_account'
352
fields:
353
id:
354
init : undefined
355
desc : 'A unique UUID for the query'
356
357
first_name:
358
init : required
359
last_name:
360
init : required
361
email_address:
362
init : required
363
password:
364
init : required
365
desc : 'must be between 6 and 64 characters in length'
366
agreed_to_terms:
367
init : required
368
desc : 'must be true for request to succeed'
369
token:
370
init : undefined # only required when token is set.
371
desc : 'account creation token - see src/dev/docker/README.md'
372
desc : """
373
Examples:
374
375
Create a new account:
376
```
377
curl -u sk_abcdefQWERTY090900000000: \\
378
-d first_name=John00 \\
379
-d last_name=Doe00 \\
380
-d email_address=jd@some_email \\
381
-d password=xyzabc09090 \\
382
-d agreed_to_terms=true https://cocalc.com/api/v1/create_account
383
```
384
385
Option `agreed_to_terms` must be present and specified as true.
386
Account creation fails if there is already an account using the
387
given email address, if `email_address` is improperly formatted,
388
and if password is fewer than 6 or more than 64 characters.
389
390
Attempting to create the same account a second time results in an error:
391
```
392
curl -u sk_abcdefQWERTY090900000000: \\
393
-d first_name=John00 \\
394
-d last_name=Doe00 \\
395
-d email_address=jd@some_email \\
396
-d password=xyzabc09090 \\
397
-d agreed_to_terms=true https://cocalc.com/api/v1/create_account
398
==> {"event":"account_creation_failed",
399
"id":"2332be03-aa7d-49a6-933a-cd9824b7331a",
400
"reason":{"email_address":"This e-mail address is already taken."}}
401
```
402
"""
403
404
message
405
event : 'account_created'
406
id : undefined
407
account_id : required
408
409
# hub --> client
410
message
411
event : 'account_creation_failed'
412
id : undefined
413
reason : required
414
415
# client --> hub
416
API message2
417
event : 'delete_account'
418
fields:
419
id:
420
init : undefined
421
desc : 'A unique UUID for the query'
422
account_id:
423
init : required
424
desc : 'account_id for account to be deleted'
425
desc : """
426
Example:
427
428
Delete an existing account:
429
```
430
curl -u sk_abcdefQWERTY090900000000: \\
431
-d account_id=99ebde5c-58f8-4e29-b6e4-b55b8fd71a1b \\
432
https://cocalc.com/api/v1/delete_account
433
==> {"event":"account_deleted","id":"9e8b68ac-08e8-432a-a853-398042fae8c9"}
434
```
435
436
Event `account_deleted` is also returned if the account was already
437
deleted before the API call, or if the account never existed.
438
439
After successful `delete_account`, the owner of the deleted account
440
will not be able to login, but will still be listed as collaborator
441
or owner on projects which the user collaborated on or owned
442
respectively.
443
"""
444
445
# hub --> client
446
message
447
event : 'account_deleted'
448
id : undefined
449
error : undefined
450
451
# client --> hub
452
message
453
id : undefined
454
event : 'sign_in'
455
email_address : required
456
password : required
457
remember_me : false
458
459
message
460
id : undefined
461
event : 'sign_in_using_auth_token'
462
auth_token : required
463
464
# hub --> client
465
message
466
id : undefined
467
event : 'remember_me_failed'
468
reason : required
469
470
# client --> hub
471
message
472
id : undefined
473
event : 'sign_in_failed'
474
email_address : required
475
reason : required
476
477
# hub --> client; sent in response to either create_account or log_in
478
message
479
event : 'signed_in'
480
id : undefined # message uuid
481
remember_me : required # true if sign in accomplished via remember_me cookie; otherwise, false.
482
hub : required # ip address (on vpn) of hub user connected to.
483
account_id : required # uuid of user's account
484
email_address : undefined # email address they signed in under
485
first_name : undefined
486
last_name : undefined
487
488
# client --> hub
489
message
490
event : 'sign_out'
491
everywhere : false
492
id : undefined
493
494
# hub --> client
495
message
496
event : 'signed_out'
497
id : undefined
498
499
# client --> hub
500
API message2
501
event : 'change_password'
502
fields:
503
id:
504
init : undefined
505
desc : 'A unique UUID for the query'
506
email_address:
507
init : required
508
desc : 'email address for account whose password is changed'
509
old_password:
510
init : ""
511
desc : ''
512
new_password:
513
init : required
514
desc : 'must be between 6 and 64 characters in length'
515
desc : """
516
Given email address and old password for an account, set a new password.
517
518
Example:
519
```
520
curl -u sk_abcdefQWERTY090900000000: \\
521
-d email_address=... \\
522
-d old_password=... \\
523
-d new_password=... \\
524
https://cocalc.com/api/v1/change_password
525
==> {"event":"changed_password","id":"41ff89c3-348e-4361-ad1d-372b55e1544a"}
526
```
527
"""
528
529
# hub --> client
530
# if error is true, that means the password was not changed; would
531
# happen if password is wrong (message:'invalid password').
532
message
533
event : 'changed_password'
534
id : undefined
535
error : undefined
536
537
# client --> hub: "please send a password reset email"
538
API message2
539
event : "forgot_password"
540
fields:
541
id:
542
init : undefined
543
desc : 'A unique UUID for the query'
544
email_address:
545
init : required
546
desc : 'email address for account requesting password reset'
547
desc : """
548
Given the email address of an existing account, send password reset email.
549
550
Example:
551
```
552
curl -u sk_abcdefQWERTY090900000000: \\
553
-d email_address=... \\
554
https://cocalc.com/api/v1/forgot_password
555
==> {"event":"forgot_password_response",
556
"id":"26ed294b-922b-47e1-8f3f-1e54d8c8e558",
557
"error":false}
558
```
559
"""
560
561
# hub --> client "a password reset email was sent, or there was an error"
562
message
563
event : "forgot_password_response"
564
id : undefined
565
error : false
566
567
# client --> hub: "reset a password using this id code that was sent in a password reset email"
568
API message2
569
event : "reset_forgot_password"
570
fields:
571
id:
572
init : undefined
573
desc : 'A unique UUID for the query'
574
reset_code:
575
init : required
576
desc : 'id code that was sent in a password reset email'
577
new_password:
578
init : required
579
desc : 'must be between 6 and 64 characters in length'
580
desc : """
581
Reset password, given reset code.
582
583
Example:
584
```
585
curl -u sk_abcdefQWERTY090900000000: \\
586
-d reset_code=35a0eea6-370a-45c3-ab2f-3210df68748f \\
587
-d new_password=qjqhddfsfj \\
588
https://cocalc.com/api/v1/reset_forgot_password
589
==> {"event":"reset_forgot_password_response","id":"85bd6027-644d-4859-9e17-5e835bd47570","error":false}
590
```
591
"""
592
593
message
594
event : "reset_forgot_password_response"
595
id : undefined
596
error : false
597
598
# client --> hub
599
API message2
600
event : 'change_email_address'
601
fields:
602
id:
603
init : undefined
604
desc : 'A unique UUID for the query'
605
account_id:
606
init : required
607
desc : 'account_id for account whose email address is changed'
608
old_email_address:
609
init : ""
610
desc : 'ignored -- deprecated'
611
new_email_address:
612
init : required
613
desc : ''
614
password:
615
init :""
616
desc : ''
617
desc:"""
618
Given the `account_id` for an account, set a new email address.
619
620
Examples:
621
622
Successful change of email address.
623
```
624
curl -u sk_abcdefQWERTY090900000000: \\
625
-d account_id=99ebde5c-58f8-4e29-b6e4-b55b8fd71a1b \\
626
-d password=secret_password \\
627
-d [email protected] \\
628
https://cocalc.com/api/v1/change_email_address
629
==> {"event":"changed_email_address",
630
"id":"8f68f6c4-9851-4b88-bd65-37cb983298e3",
631
"error":false}
632
```
633
634
Fails if new email address is already in use.
635
636
```
637
curl -u sk_abcdefQWERTY090900000000: \\
638
-d account_id=99ebde5c-58f8-4e29-b6e4-b55b8fd71a1b \\
639
-d password=secret_password \\
640
-d [email protected] \\
641
https://cocalc.com/api/v1/change_email_address
642
==> {"event":"changed_email_address",
643
"id":"4501f022-a57c-4aaf-9cd8-af0eb05ebfce",
644
"error":"email_already_taken"}
645
```
646
647
**Note:** `account_id` and `password` must match the `id` of the current login.
648
"""
649
650
# hub --> client
651
message
652
event : 'changed_email_address'
653
id : undefined
654
error : false # some other error
655
ttl : undefined # if user is trying to change password too often, this is time to wait
656
657
658
659
# Unlink a passport auth for this account.
660
# client --> hub
661
API message2
662
event : 'unlink_passport'
663
fields:
664
strategy:
665
init : required
666
desc : 'passport strategy'
667
id:
668
init : required
669
desc : 'numeric id for user and passport strategy'
670
desc:"""
671
Unlink a passport auth for the account.
672
673
Strategies are defined in the database and may be viewed at [/auth/strategies](https://cocalc.com/auth/strategies).
674
675
Example:
676
677
Get passport id for some strategy for current user.
678
```
679
curl -u sk_abcdefQWERTY090900000000: \\
680
-H "Content-Type: application/json" \\
681
-d '{"query":{"accounts":{"account_id":"e6993694-820d-4f78-bcc9-10a8e336a88d","passports":null}}}' \\
682
https://cocalc.com/api/v1/query
683
==> {"query":{"accounts":{"account_id":"e6993694-820d-4f78-bcc9-10a8e336a88d",
684
"passports":{"facebook-14159265358":{"id":"14159265358",...}}}},
685
"multi_response":false,
686
"event":"query",
687
"id":"a2554ec8-665b-495b-b0e2-8e248b54eb94"}
688
```
689
690
Unlink passport for that strategy and id.
691
```
692
curl -u sk_abcdefQWERTY090900000000: \\
693
-d strategy=facebook \\
694
-d id=14159265358 \\
695
https://cocalc.com/api/v1/unlink_passport
696
==> {"event":"success",
697
"id":"14159265358"}
698
```
699
700
Note that success is returned regardless of whether or not passport was linked
701
for the given strategy and id before issuing the API command.
702
"""
703
message
704
event : 'error'
705
id : undefined
706
error : undefined
707
708
message
709
event : 'success'
710
id : undefined
711
712
# You need to reconnect.
713
message
714
event : 'reconnect'
715
id : undefined
716
reason : undefined # optional to make logs more informative
717
718
719
######################################################################################
720
# This is a message that goes
721
# hub --> client
722
# In response, the client grabs "/cookies?id=...,set=...,get=..." via an AJAX call.
723
# During that call the server can get/set HTTP-only cookies.
724
# (Note that the /cookies url gets customized by base_url.)
725
######################################################################################
726
message
727
event : 'cookies'
728
id : required
729
url : "/cookies"
730
get : undefined # name of a cookie to get
731
set : undefined # name of a cookie to set
732
value : undefined # value to set cookie to
733
734
###
735
736
Project Server <---> Hub interaction
737
738
These messages are mainly focused on working with individual projects.
739
740
Architecture:
741
742
* The database stores a files object (with the file tree), logs
743
(of each branch) and a sequence of git bundles that when
744
combined together give the complete history of the repository.
745
Total disk usage per project is limited by hard/soft disk quota,
746
and includes the space taken by the revision history (the .git
747
directory).
748
749
* A project should only be opened by at most one project_server at
750
any given time (not implemented: if this is violated then we'll
751
merge the resulting conflicting repo's.)
752
753
* Which project_server that has a project opened is stored in the
754
database. If a hub cannot connect to a given project server,
755
the hub assigns a new project_server for the project and opens
756
the project on the new project_server. (The error also gets
757
logged to the database.) All hubs will use this new project
758
server henceforth.
759
760
###
761
762
# The open_project message causes the project_server to create a new
763
# project or prepare to receive one (as a sequence of blob messages)
764
# from a hub.
765
#
766
# hub --> project_server
767
message
768
event : 'open_project'
769
id : required
770
project_id : required # uuid of the project, which impacts
771
# where project is extracted, etc.
772
quota : required # Maximum amount of disk space/inodes this
773
# project can use. This is an object
774
# {disk:{soft:megabytes, hard:megabytes}, inode:{soft:num, hard:num}}
775
idle_timeout : required # A time in seconds; if the project_server
776
# does not receive any messages related
777
# to this project for this many seconds,
778
# then it does the same thing as when
779
# receiving a 'close_project' message.
780
ssh_public_key: required # ssh key of the one UNIX user that is allowed to access this account (this is running the hub).
781
782
# A project_server sends the project_opened message to the hub once
783
# the project_server has received and unbundled all bundles that
784
# define a project.
785
# project_server --> hub
786
message
787
event : 'project_opened'
788
id : required
789
790
######################################################################
791
# Execute a shell command in a given project
792
######################################################################
793
794
# client --> project
795
API message2
796
event : 'project_exec'
797
fields:
798
id:
799
init : undefined
800
desc : 'A unique UUID for the query'
801
project_id:
802
init : required
803
desc : 'id of project where command is to be executed'
804
path:
805
init : ''
806
desc : 'path of working directory for the command'
807
command:
808
init : required
809
desc : 'command to be executed'
810
args:
811
init : []
812
desc : 'command line options for the command'
813
timeout:
814
init : 10
815
desc : 'maximum allowed time, in seconds'
816
max_output:
817
init : undefined
818
desc : 'maximum number of characters in the output'
819
bash:
820
init : false
821
desc : 'if true, args are ignored and command is run as a bash command'
822
err_on_exit:
823
init : true
824
desc : 'if exit code is nonzero send error return message instead of the usual output'
825
desc: """
826
Execute a shell command in a given project.
827
828
Examples:
829
830
Simple built-in shell command.
831
```
832
curl -u sk_abcdefQWERTY090900000000: \\
833
-d command=pwd \\
834
-d project_id=e49e86aa-192f-410b-8269-4b89fd934fba \\
835
https://cocalc.com/api/v1/project_exec
836
==> {"event":"project_exec_output",
837
"id":"8a78a37d-b2fb-4e29-94ae-d66acdeac949",
838
"stdout":"/projects/e49e86aa-192f-410b-8269-4b89fd934fba\\n","stderr":"","exit_code":0}
839
```
840
841
Shell command with different working directory.
842
```
843
curl -u sk_abcdefQWERTY090900000000: \\
844
-d command=pwd \\
845
-d path=Private \\
846
-d project_id=e49e86aa-192f-410b-8269-4b89fd934fba \\
847
https://cocalc.com/api/v1/project_exec
848
==> {"event":"project_exec_output",
849
"id":"8a78a37d-b2fb-4e29-94ae-d66acdeac949",
850
"stdout":"/projects/e49e86aa-192f-410b-8269-4b89fd934fba/Private\\n","stderr":"","exit_code":0}
851
```
852
853
Command line arguments specified by 'args' option. Note JSON format for request parameters.
854
```
855
curl -u sk_abcdefQWERTY090900000000: \\
856
-H 'Content-Type: application/json' \\
857
-d '{"command":"echo","args":["xyz","abc"],"project_id":"e49e86aa-192f-410b-8269-4b89fd934fba"}' \\
858
https://cocalc.com/api/v1/project_exec
859
==> {"event":"project_exec_output",
860
"id":"39289ba7-0333-48ad-984e-b25c8b8ffa0e",
861
"stdout":"xyz abc\\n",
862
"stderr":"",
863
"exit_code":0}
864
```
865
866
Limiting output of the command to 3 characters.
867
```
868
curl -u sk_abcdefQWERTY090900000000: \\
869
-H 'Content-Type: application/json' \\
870
-d '{"command":"echo","args":["xyz","abc"],"max_output":3,"project_id":"e49e86aa-192f-410b-8269-4b89fd934fba"}' \\
871
https://cocalc.com/api/v1/project_exec
872
==> {"event":"project_exec_output",
873
"id":"02feab6c-a743-411a-afca-8a23b58988a9",
874
"stdout":"xyz (truncated at 3 characters)",
875
"stderr":"",
876
"exit_code":0}
877
```
878
879
Setting a timeout for the command.
880
```
881
curl -u sk_abcdefQWERTY090900000000: \\
882
-H 'Content-Type: application/json' \\
883
-d '{"command":"sleep 5","timeout":2,"project_id":"e49e86aa-192f-410b-8269-4b89fd934fba"}' \\
884
https://cocalc.com/api/v1/project_exec
885
==> {"event":"error",
886
"id":"86fea3f0-6a90-495b-a541-9c14a25fbe58",
887
"error":"Error executing command 'sleep 5' with args '' -- killed command 'bash /tmp/f-11757-1677-8ei2z0.t4fex0qkt9', , "}
888
```
889
890
Notes:
891
- Argument `command` may invoke an executable file or a built-in shell command. It may include
892
a path and command line arguments.
893
- If option `args` is provided, options must be sent as a JSON object.
894
- Argument `path` is optional. When provided, `path` is relative to home directory in target project
895
and specifies the working directory in which the command will be run.
896
"""
897
898
# project --> client
899
message
900
event : 'project_exec_output'
901
id : required
902
stdout : required
903
stderr : required
904
exit_code : required
905
906
907
######################################################################
908
# Jupyter server
909
######################################################################
910
911
# starts jupyter hub server and reports the port it is running on
912
# hub <--> project
913
message
914
event : 'jupyter_port'
915
port : undefined # gets set in response
916
id : undefined
917
mathjax_url : undefined # e.g. '/static/mathjax-2.6.1/MathJax.js'
918
919
#############################################################################
920
921
922
# The read_file_from_project message is sent by the hub to request
923
# that the project_server read a file from a project and send it back
924
# to the hub as a blob. Also sent by client to hub to request a file
925
# or directory. If path is a directory, the optional archive field
926
# specifies how to create a single file archive, with supported
927
# options including: 'tar', 'tar.bz2', 'tar.gz', 'zip', '7z'.
928
#
929
# client --> hub --> project_server
930
message
931
event : 'read_file_from_project'
932
id : undefined
933
project_id : required
934
path : required
935
archive : 'tar.bz2'
936
937
# The file_read_from_project message is sent by the project_server
938
# when it finishes reading the file from disk.
939
# project_server --> hub
940
message
941
event : 'file_read_from_project'
942
id : required
943
data_uuid : required # The project_server will send the raw data of the file as a blob with this uuid.
944
archive : undefined # if defined, means that file (or directory) was archived (tarred up) and this string was added to end of filename.
945
946
947
# hub --> client
948
message
949
event : 'temporary_link_to_file_read_from_project'
950
id : required
951
url : required
952
953
# The client sends this message to the hub in order to write (or
954
# create) a plain text file (binary files not allowed, since sending
955
# them via JSON makes no sense).
956
# client --> hub
957
API message2
958
event : 'read_text_file_from_project'
959
fields:
960
id:
961
init : undefined
962
desc : 'A unique UUID for the query'
963
project_id:
964
init : required
965
desc : 'id of project containing file to be read'
966
path:
967
init : required
968
desc : 'path to file to be read in target project'
969
desc: """
970
Read a text file in the project whose id is supplied.
971
User must be owner or collaborator in the target project.
972
Argument 'path' is relative to home directory in target project.
973
Unix user in the target project must have permissions to read file
974
and containing directories if they do not already exist.
975
976
Example:
977
978
Read a text file.
979
```
980
curl -u sk_abcdefQWERTY090900000000: \\
981
-d project_id=e49e86aa-192f-410b-8269-4b89fd934fba \\
982
-d path=Assignments/A1/h1.txt \\
983
https://cocalc.com/api/v1/read_text_file_from_project
984
==> {"event":"text_file_read_from_project",
985
"id":"481d6055-5609-450f-a229-480e518b2f84",
986
"content":"hello"}
987
```
988
"""
989
990
# hub --> client
991
message
992
event : 'text_file_read_from_project'
993
id : required
994
content : required
995
996
# The write_file_to_project message is sent from the hub to the
997
# project_server to tell the project_server to write a file to a
998
# project. If the path includes directories that don't exists,
999
# they are automatically created (this is in fact the only way
1000
# to make a new directory).
1001
# hub --> project_server
1002
message
1003
event : 'write_file_to_project'
1004
id : required
1005
project_id : required
1006
path : required
1007
data_uuid : required # hub sends raw data as a blob with this uuid immediately.
1008
1009
# The client sends this message to the hub in order to write (or
1010
# create) a plain text file (binary files not allowed, since sending
1011
# them via JSON makes no sense).
1012
# client --> hub
1013
API message2
1014
event : 'write_text_file_to_project'
1015
fields:
1016
id:
1017
init : undefined
1018
desc : 'A unique UUID for the query'
1019
project_id:
1020
init : required
1021
desc : 'id of project where file is created'
1022
path:
1023
init : required
1024
desc : 'path to file, relative to home directory in destination project'
1025
content:
1026
init : required
1027
desc : 'contents of the text file to be written'
1028
desc:"""
1029
Create a text file in the target project.
1030
User must be owner or collaborator in the target project.
1031
Directories containing the file are created if they do not exist already.
1032
Unix user in the target project must have permissions to create file
1033
and containing directories if they do not already exist.
1034
If a file already exists at the destination path, it is overwritten.
1035
1036
Example:
1037
1038
Create a text file.
1039
```
1040
curl -u sk_abcdefQWERTY090900000000: \\
1041
-d project_id=e49e86aa-192f-410b-8269-4b89fd934fba \\
1042
-d "content=hello$'\\n'world" \\
1043
-d path=Assignments/A1/h1.txt \\
1044
https://cocalc.com/api/v1/write_text_file_to_project
1045
```
1046
"""
1047
1048
# The file_written_to_project message is sent by a project_server to
1049
# confirm successful write of the file to the project.
1050
# project_server --> hub
1051
message
1052
event : 'file_written_to_project'
1053
id : required
1054
1055
############################################
1056
# Managing multiple projects
1057
############################################
1058
1059
# client --> hub
1060
API message2
1061
event : 'create_project'
1062
fields:
1063
id:
1064
init : undefined
1065
desc : 'A unique UUID for the query'
1066
title :
1067
init : ''
1068
desc : 'project title'
1069
description:
1070
init : ''
1071
desc : 'project description'
1072
start :
1073
init : false
1074
desc : 'start running the moment the project is created -- uses more resources, but possibly better user experience'
1075
desc : """
1076
Example:
1077
```
1078
curl -u sk_abcdefQWERTY090900000000: \\
1079
-d title='MY NEW PROJECT' \\
1080
-d description='sample project' \\
1081
https://cocalc.com/api/v1/create_project
1082
== > {"event":"project_created",
1083
"id":"0b4df293-d518-45d0-8a3c-4281e501b85e",
1084
"project_id":"07897899-6bbb-4fbc-80a7-3586c43348d1"}
1085
```
1086
"""
1087
1088
# hub --> client
1089
message
1090
event : 'project_created'
1091
id : required
1092
project_id : required
1093
1094
1095
# hub --> client(s)
1096
message
1097
event : 'project_list_updated'
1098
1099
## search ---------------------------
1100
1101
# client --> hub
1102
API message2
1103
event : 'user_search'
1104
fields:
1105
id:
1106
init : undefined
1107
desc : 'A unique UUID for the query'
1108
query:
1109
init : required
1110
desc : "comma separated list of email addresses or strings such as 'foo bar'"
1111
limit:
1112
init : 20
1113
desc : 'maximum number of results returned'
1114
desc: """
1115
There are two possible item types in the query list: email addresses
1116
and strings that are not email addresses. An email query item will return
1117
account id, first name, last name, and email address for the unique
1118
account with that email address, if there is one. A string query item
1119
will return account id, first name, and last name for all matching
1120
accounts.
1121
1122
We do not reveal email addresses of users queried by name.
1123
1124
String query matches first and last names that start with the given string.
1125
If a string query item consists of two strings separated by space,
1126
the search will return accounts in which the first name begins with one
1127
of the two strings and the last name begins with the other.
1128
String and email queries may be mixed in the list for a single
1129
user_search call. Searches are case-insensitive.
1130
1131
Note: there is a hard limit of 50 returned items in the results.
1132
1133
Examples:
1134
1135
Search for account by email.
1136
```
1137
curl -u sk_abcdefQWERTY090900000000: \\
1138
-d [email protected] \\
1139
https://cocalc.com/api/v1/user_search
1140
==> {"event":"user_search_results",
1141
"id":"3818fa50-b892-4167-b9d9-d22d521b36af",
1142
"results":[{"account_id":"96c523b8-321e-41a3-9523-39fde95dc71d",
1143
"first_name":"John",
1144
"last_name":"Doe",
1145
"email_address":"[email protected]"}
1146
```
1147
1148
Search for at most 3 accounts where first and last name begin with 'foo' or 'bar'.
1149
```
1150
curl -u sk_abcdefQWERTY090900000000: \\
1151
-d 'query=foo bar'\\
1152
-d limit=3 \\
1153
https://cocalc.com/api/v1/user_search
1154
==> {"event":"user_search_results",
1155
"id":"fd9b025b-25d0-4e27-97f4-2c080bb07155",
1156
"results":[{"account_id":"1a842a67-eed3-405d-a222-2f23a33f675e",
1157
"first_name":"foo",
1158
"last_name":"bar"},
1159
{"account_id":"0e9418a7-af6a-4004-970a-32fafe733f29",
1160
"first_name":"bar123",
1161
"last_name":"fooxyz"},
1162
{"account_id":"93f8131c-6c21-401a-897d-d4abd9c6c225",
1163
"first_name":"Foo",
1164
"last_name":"Bar"}]}
1165
```
1166
1167
The same result as the last example above would be returned with a
1168
search string of 'bar foo'.
1169
A name of "Xfoo YBar" would not match.
1170
1171
Note that email addresses are not returned for string search items.
1172
1173
Email and string search types may be mixed in a single query:
1174
```
1175
curl -u sk_abcdefQWERTY090900000000: \\
1176
-d 'query=foo bar,[email protected]' \\
1177
-d limit=4 \\
1178
https://cocalc.com/api/v1/user_search
1179
```
1180
"""
1181
1182
# hub --> client
1183
message
1184
event : 'user_search_results'
1185
id : undefined
1186
results : required # list of {first_name:, last_name:, account_id:} objects.
1187
1188
# hub --> client
1189
message
1190
event : 'project_users'
1191
id : undefined
1192
users : required # list of {account_id:?, first_name:?, last_name:?, mode:?, state:?}
1193
1194
API message2
1195
event : 'invite_collaborator'
1196
fields:
1197
id:
1198
init : undefined
1199
desc : 'A unique UUID for the query'
1200
project_id:
1201
init : required
1202
desc : 'project_id of project into which user is invited'
1203
account_id:
1204
init : required
1205
desc : 'account_id of invited user'
1206
desc : """
1207
Invite a user who already has a CoCalc account to
1208
become a collaborator on a project. You must be owner
1209
or collaborator on the target project.
1210
1211
Example:
1212
```
1213
curl -u sk_abcdefQWERTY090900000000: \\
1214
-d account_id=99ebde5c-58f8-4e29-b6e4-b55b8fd71a1b \\
1215
-d project_id=18955da4-4bfa-4afa-910c-7f2358c05eb8 \\
1216
https://cocalc.com/api/v1/invite_collaborator
1217
==> {"event":"success",
1218
"id":"e80fd64d-fd7e-4cbc-981c-c0e8c843deec"}
1219
```
1220
"""
1221
1222
API message2
1223
event : 'remove_collaborator'
1224
fields:
1225
id:
1226
init : undefined
1227
desc : 'A unique UUID for the query'
1228
project_id:
1229
init : required
1230
desc : 'project_id of project from which user is removed'
1231
account_id:
1232
init : required
1233
desc : 'account_id of removed user'
1234
desc : """
1235
Remove a user from a CoCalc project.
1236
You must be owner or collaborator on the target project.
1237
You cannot remove the project owner.
1238
1239
Example:
1240
```
1241
curl -u sk_abcdefQWERTY090900000000: \\
1242
-d account_id=99ebde5c-58f8-4e29-b6e4-b55b8fd71a1b \\
1243
-d project_id=18955da4-4bfa-4afa-910c-7f2358c05eb8 \\
1244
https://cocalc.com/api/v1/remove_collaborator
1245
==> {"event":"success",
1246
"id":"e80fd64d-fd7e-4cbc-981c-c0e8c843deec"}
1247
```
1248
"""
1249
1250
# DANGER -- can be used to spam people.
1251
API message2
1252
event : 'invite_noncloud_collaborators'
1253
fields :
1254
id:
1255
init : undefined
1256
desc : 'A unique UUID for the query'
1257
project_id:
1258
init : required
1259
desc : 'project_id of project into which users are invited'
1260
to:
1261
init : required
1262
desc : 'comma- or semicolon-delimited string of email addresses'
1263
email:
1264
init : required
1265
desc : 'body of the email to be sent, may include HTML markup'
1266
title:
1267
init : required
1268
desc : 'string that will be used for project title in the email'
1269
link2proj:
1270
init : required
1271
desc : 'URL for the target project'
1272
replyto:
1273
init : undefined
1274
desc : 'Reply-To email address'
1275
replyto_name:
1276
init : undefined
1277
desc : 'Reply-To name'
1278
subject:
1279
init : undefined
1280
desc : 'email Subject'
1281
desc : """
1282
Invite users who do not already have a CoCalc account
1283
to join a project.
1284
An invitation email is sent to each user in the `to`
1285
option.
1286
Invitation is not sent if there is already a CoCalc
1287
account with the given email address.
1288
You must be owner or collaborator on the target project.
1289
1290
Limitations:
1291
- Total length of the request message must be less than or equal to 1024 characters.
1292
- Length of each email address must be less than 128 characters.
1293
1294
1295
Example:
1296
```
1297
curl -u sk_abcdefQWERTY090900000000: \\
1298
-d project_id=18955da4-4bfa-4afa-910c-7f2358c05eb8 \\
1299
-d [email protected] \\
1300
-d 'email=Please sign up and join this project.' \\
1301
-d 'title=Class Project' \\
1302
-d link2proj=https://cocalc.com/projects/18955da4-4bfa-4afa-910c-7f2358c05eb8 \\
1303
https://cocalc.com/api/v1/invite_noncloud_collaborators
1304
==> {"event":"invite_noncloud_collaborators_resp",
1305
"id":"39d7203d-89b1-4145-8a7a-59e41d5682a3",
1306
"mesg":"Invited [email protected] to collaborate on a project."}
1307
```
1308
1309
Email sent by the previous example:
1310
1311
```
1312
To: [email protected]
1313
From: CoCalc <[email protected]
1314
Reply-To: [email protected]
1315
Subject: CoCalc Invitation
1316
1317
Please sign up and join this project.<br/><br/>\\n<b>
1318
To accept the invitation, please sign up at\\n
1319
<a href='https://cocalc.com'>https://cocalc.com</a>\\n
1320
using exactly the email address '[email protected]'.\\n
1321
Then go to <a href='https://cocalc.com/projects/18955da4-4bfa-4afa-910c-7f2358c05eb8'>
1322
the project 'Team Project'</a>.</b><br/>
1323
```
1324
"""
1325
1326
message
1327
event : 'invite_noncloud_collaborators_resp'
1328
id : undefined
1329
mesg : required
1330
1331
###
1332
Send/receive the current webapp code version number.
1333
1334
This can be used by clients to suggest a refresh/restart.
1335
The client may sends their version number on connect.
1336
If the client sends their version and later it is out of date
1337
due to an update, the server sends a new version number update
1338
message to that client.
1339
###
1340
# client <---> hub
1341
message
1342
event : 'version'
1343
version : undefined # gets filled in by the hub
1344
min_version : undefined # if given, then client version must be at least min_version to be allowed to connect.
1345
1346
#############################################
1347
#
1348
# Message sent in response to attempt to save a blob
1349
# to the database.
1350
#
1351
# hub --> local_hub --> sage_server
1352
#
1353
#############################################
1354
message
1355
event : 'save_blob'
1356
id : undefined
1357
sha1 : required # the sha-1 hash of the blob that we just processed
1358
ttl : undefined # ttl in seconds of the blob if saved; 0=infinite
1359
error : undefined # if not saving, a message explaining why.
1360
1361
1362
# remove the ttls from blobs in the blobstore.
1363
# client --> hub
1364
message
1365
event : 'remove_blob_ttls'
1366
id : undefined
1367
uuids : required # list of sha1 hashes of blobs stored in the blobstore
1368
1369
# DEPRECATED -- used by bup_server
1370
message
1371
event : 'storage'
1372
action : required # open, save, snapshot, latest_snapshot, close
1373
project_id : undefined
1374
param : undefined
1375
id : undefined
1376
1377
message
1378
event : 'projects_running_on_server'
1379
id : undefined
1380
projects : undefined # for response
1381
1382
1383
###
1384
Direct messaging between browser client and local_hub,
1385
forwarded on by global hub after ensuring write access.
1386
###
1387
message
1388
event : 'local_hub'
1389
project_id : required
1390
timeout : undefined
1391
id : undefined
1392
multi_response : false
1393
message : required # arbitrary message
1394
1395
1396
###########################################################
1397
#
1398
# Copy a path from one project to another.
1399
#
1400
###########################################################
1401
API message2
1402
event : 'copy_path_between_projects'
1403
fields:
1404
id:
1405
init : undefined
1406
desc : 'A unique UUID for the query'
1407
src_project_id:
1408
init : required
1409
desc : 'id of source project'
1410
src_path:
1411
init : required
1412
desc : 'relative path of directory or file in the source project'
1413
target_project_id:
1414
init : required
1415
desc : 'id of target project'
1416
target_path:
1417
init : undefined
1418
desc : 'defaults to src_path'
1419
overwrite_newer:
1420
init : false
1421
desc : 'overwrite newer versions of file at destination (destructive)'
1422
delete_missing:
1423
init : false
1424
desc : 'delete files in dest that are missing from source (destructive)'
1425
backup:
1426
init : false
1427
desc : 'make ~ backup files instead of overwriting changed files'
1428
timeout:
1429
init : undefined
1430
desc : 'seconds to wait before reporting "error" (though copy could still succeed)'
1431
exclude_history:
1432
init : false
1433
desc : 'if true, exclude all files of the form *.sage-history'
1434
desc : """
1435
Copy a file or directory from one project to another. User must be
1436
owner or collaborator on both projects.
1437
1438
Note: the `timeout` option is passed to a call to the `rsync` command.
1439
If no data is transferred for the specified number of seconds, then
1440
the copy terminates. The default is 0, which means no timeout.
1441
1442
Relative paths (paths not beginning with '/') are relative to the user's
1443
home directory in source and target projects.
1444
1445
Example:
1446
1447
Copy file `A/doc.txt` from source project to target project.
1448
Folder `A` will be created in target project if it does not exist already.
1449
1450
```
1451
curl -u sk_abcdefQWERTY090900000000: \\
1452
-d src_project_id=e49e86aa-192f-410b-8269-4b89fd934fba \\
1453
-d src_path=A/doc.txt \\
1454
-d target_project_id=2aae4347-214d-4fd1-809c-b327150442d8 \\
1455
https://cocalc.com/api/v1/copy_path_between_projects
1456
==> {"event":"success",
1457
"id":"45d851ac-5ea0-4aea-9997-99a06c054a60"}
1458
```
1459
"""
1460
1461
1462
#############################################
1463
# Admin Functionality
1464
#############################################
1465
1466
# client --> hub; will result in an error if the user is not in the admin group.
1467
message
1468
event : 'project_set_quotas'
1469
id : undefined
1470
project_id : required # the id of the project's id to set.
1471
memory : undefined # RAM in megabytes
1472
cpu_shares : undefined # fair sharing with everybody is 256, not 1 !!!
1473
cores : undefined # integer max number of cores user can use (>=1)
1474
disk_quota : undefined # disk quota in megabytes
1475
mintime : undefined # time in **seconds** until idle projects are terminated
1476
network : undefined # 1 or 0; if 1, full access to outside network
1477
member_host : undefined # 1 or 0; if 1, project will be run on a members-only machine
1478
1479
###
1480
Printing Files
1481
###
1482
message
1483
event : "print_to_pdf"
1484
id : undefined
1485
path : required
1486
options : undefined
1487
1488
message
1489
event : 'printed_to_pdf'
1490
id : undefined
1491
path : required
1492
1493
1494
###
1495
Ping/pong -- used for clock sync, etc.
1496
###
1497
API message2
1498
event : 'ping'
1499
fields:
1500
id:
1501
init : undefined
1502
desc : 'A unique UUID for the query'
1503
desc : """
1504
Test API connection, return time as ISO string when server responds to ping.
1505
1506
Examples:
1507
1508
Omitting request id:
1509
```
1510
curl -X POST -u sk_abcdefQWERTY090900000000: https://cocalc.com/api/v1/ping
1511
==> {"event":"pong","id":"c74afb40-d89b-430f-836a-1d889484c794","now":"2017-05-24T13:29:11.742Z"}
1512
```
1513
1514
Using `uuid` shell command to create a request id:
1515
```
1516
uuid
1517
==> 553f2815-1508-416d-8e69-2dde5af3aed8
1518
curl -u sk_abcdefQWERTY090900000000: https://cocalc.com/api/v1/ping -d id=553f2815-1508-416d-8e69-2dde5af3aed8
1519
==> {"event":"pong","id":"553f2815-1508-416d-8e69-2dde5af3aed8","now":"2017-05-24T13:47:21.312Z"}
1520
```
1521
1522
Using JSON format to provide request id:
1523
```
1524
curl -u sk_abcdefQWERTY090900000000: -H "Content-Type: application/json" \\
1525
-d '{"id":"8ec4ac73-2595-42d2-ad47-0b9641043b46"}' https://cocalc.com/api/v1/ping
1526
==> {"event":"pong","id":"8ec4ac73-2595-42d2-ad47-0b9641043b46","now":"2017-05-24T17:15:59.288Z"}
1527
```
1528
"""
1529
1530
message
1531
event : 'pong'
1532
id : undefined
1533
now : undefined # timestamp
1534
1535
1536
1537
###
1538
Reading listings and files from projects
1539
without invoking the project server and
1540
write auth requirement. Instead the given
1541
path in the project must be public. These
1542
functions don't even assume the client has
1543
logged in.
1544
###
1545
1546
# public request of listing of files in a project.
1547
API message2
1548
event : 'public_get_directory_listing'
1549
fields:
1550
id:
1551
init : undefined
1552
desc : 'A unique UUID for the query'
1553
project_id:
1554
init : required
1555
desc : 'id of project containing public file to be read'
1556
path:
1557
init : required
1558
desc : 'path of directory in target project'
1559
hidden:
1560
init : false
1561
desc : 'show hidden files'
1562
time:
1563
init : false
1564
desc : 'sort by timestamp, with newest first'
1565
start:
1566
init : 0
1567
desc : ''
1568
limit:
1569
init : -1
1570
desc : ''
1571
desc:"""
1572
Given a project id and relative path (i.e. not beginning with a slash),
1573
list all public files and subdirectories under that path.
1574
Path is required, but may be the empty string, in which case
1575
a public listing of the home directory in the target project is
1576
returned.
1577
1578
Examples:
1579
1580
Get public directory listing. Directory "Public" is shared and
1581
contains one file "hello.txt" and one subdirectory "p2".
1582
1583
```
1584
curl -u sk_abcdefQWERTY090900000000: \\
1585
-d path=Public \\
1586
-d project_id=9a19cca3-c53d-4c7c-8c0f-e166aada7bb6 \\
1587
https://cocalc.com/api/v1/public_get_directory_listing
1588
==> {"event":"public_directory_listing",
1589
"id":"3e576b3b-b673-4d5c-9bce-780883f92958",
1590
"result":{"files":[{"size":41,"name":"hello.txt","mtime":1496430932},
1591
{"isdir":true,"name":"p2","mtime":1496461616}]}
1592
```
1593
"""
1594
1595
message
1596
event : 'public_directory_listing'
1597
id : undefined
1598
result : required
1599
1600
# public request of contents of a text file in project
1601
API message2
1602
event : 'public_get_text_file'
1603
fields:
1604
id:
1605
init : undefined
1606
desc : 'A unique UUID for the query'
1607
project_id:
1608
init : required
1609
desc : 'id of project containing public file to be read'
1610
path:
1611
init : required
1612
desc : 'path to file to be read in target project'
1613
desc: """
1614
Read a public (shared) text file in the project whose id is supplied.
1615
User does not need to be owner or collaborator in the target project
1616
and does not need to be logged into CoCalc.
1617
Argument `path` is relative to home directory in target project.
1618
1619
Examples
1620
1621
Read a public file.
1622
```
1623
curl -u sk_abcdefQWERTY090900000000: \\
1624
-d project_id=e49e86aa-192f-410b-8269-4b89fd934fba \\
1625
-d path=Public/hello.txt
1626
https://cocalc.com/api/v1/public_get_text_file
1627
==> {"event":"public_text_file_contents",
1628
"id":"2d0e2faa-893a-44c1-9f64-59203bbbb017",
1629
"data":"hello world\\nToday is Friday\\n"}
1630
```
1631
1632
Attempt to read a file which is not public.
1633
```
1634
curl -u sk_abcdefQWERTY090900000000: \\
1635
-d project_id=e49e86aa-192f-410b-8269-4b89fd934fba \\
1636
-d path=Private/hello.txt
1637
https://cocalc.com/api/v1/public_get_text_file
1638
==> {"event":"error","id":"0288b7d0-dda9-4895-87ba-aa71929b2bfb",
1639
"error":"path 'Private/hello.txt' of project with id 'e49e86aa-192f-410b-8269-4b89fd934fba' is not public"}+
1640
```
1641
"""
1642
1643
message
1644
event : 'public_text_file_contents'
1645
id : undefined
1646
data : required
1647
1648
API message2
1649
event : 'copy_public_path_between_projects'
1650
fields:
1651
id:
1652
init : undefined
1653
desc : 'A unique UUID for the query'
1654
src_project_id:
1655
init : required
1656
desc : 'id of source project'
1657
src_path:
1658
init : required
1659
desc : 'relative path of directory or file in the source project'
1660
target_project_id:
1661
init : required
1662
desc : 'id of target project'
1663
target_path:
1664
init : undefined
1665
desc : 'defaults to src_path'
1666
overwrite_newer:
1667
init : false
1668
desc : 'overwrite newer versions of file at destination (destructive)'
1669
delete_missing:
1670
init : false
1671
desc : 'delete files in dest that are missing from source (destructive)'
1672
backup:
1673
init : false
1674
desc : 'make ~ backup files instead of overwriting changed files'
1675
timeout:
1676
init : undefined
1677
desc : 'how long to wait for the copy to complete before reporting error (though it could still succeed)'
1678
exclude_history:
1679
init : false
1680
desc : 'if true, exclude all files of the form *.sage-history'
1681
desc : """
1682
Copy a file or directory from public project to a project for which the
1683
user is owner or collaborator.
1684
1685
Note: the `timeout` option is passed to a call to the `rsync` command.
1686
If no data is transferred for the specified number of seconds, then
1687
the copy terminates. The default is 0, which means no timeout.
1688
1689
Example:
1690
1691
Copy public file `PUBLIC/doc.txt` from source project to private file
1692
`A/sample.txt` in target project.
1693
1694
```
1695
curl -u sk_abcdefQWERTY090900000000: \\
1696
-d src_project_id=e49e86aa-192f-410b-8269-4b89fd934fba \\
1697
-d src_path=PUBLIC/doc.txt \\
1698
-d target_project_id=2aae4347-214d-4fd1-809c-b327150442d8 \\
1699
-d target_path=A/sample.txt \\
1700
https://cocalc.com/api/v1/copy_public_path_between_projects
1701
==> {"event":"success",
1702
"id":"45d851ac-5ea0-4aea-9997-99a06c054a60"}
1703
```
1704
"""
1705
1706
API message2
1707
event : 'log_client_error'
1708
fields:
1709
id:
1710
init : undefined
1711
desc : 'A unique UUID for the query'
1712
error:
1713
init : required
1714
desc : 'error string'
1715
desc : """
1716
Log an error so that CoCalc support can look at it.
1717
1718
In the following example, an explicit message id
1719
is provided for future reference.
1720
```
1721
curl -u sk_abcdefQWERTY090900000000: \\
1722
-d id=34a424dc-1731-4b31-ba3d-fc8a484980d9 \\
1723
-d "error=cannot load library xyz" \\
1724
https://cocalc.com/api/v1/log_client_error
1725
==> {"event":"success",
1726
"id":"34a424dc-1731-4b31-ba3d-fc8a484980d9"}
1727
```
1728
1729
Note: the above API call will create the following record in the
1730
`client_error_log` database table. This table is not readable
1731
via the API and is intended for use by CoCalc support only:
1732
```
1733
[{"id":"34a424dc-1731-4b31-ba3d-fc8a484980d9",
1734
"event":"error",
1735
"error":"cannot load library xyz",
1736
"account_id":"1c87a139-9e13-4cdd-b02c-e7d41dcfe921",
1737
"time":"2017-07-06T02:32:41.176Z"}]
1738
```
1739
"""
1740
1741
message
1742
event : 'webapp_error'
1743
name : required # string
1744
message : required # string
1745
comment : undefined # string
1746
stacktrace : undefined # string
1747
file : undefined # string
1748
path : undefined # string
1749
lineNumber : undefined # int
1750
columnNumber : undefined # int
1751
severity : undefined # string
1752
browser : undefined # string, how feature.coffee detected the browser
1753
mobile : undefined # boolean, feature.coffee::IS_MOBILE
1754
responsive : undefined # boolean, feature.coffee::is_responsive_mode
1755
user_agent : undefined # string
1756
smc_version : undefined # string
1757
build_date : undefined # string
1758
smc_git_rev : undefined # string
1759
uptime : undefined # string
1760
start_time : undefined # timestamp
1761
1762
1763
###
1764
Stripe integration
1765
###
1766
1767
# Set the stripe payment method for this user.
1768
1769
# customer info
1770
API message
1771
event : 'stripe_get_customer'
1772
id : undefined
1773
1774
API message
1775
event : 'stripe_customer'
1776
id : undefined
1777
customer : undefined # if user already has a stripe customer account, info about it.
1778
stripe_publishable_key : undefined # if stripe is configured for this SMC instance, this is the public API key.
1779
1780
1781
# card
1782
API message
1783
event : 'stripe_create_source'
1784
id : undefined
1785
token : required
1786
1787
API message
1788
event : 'stripe_delete_source'
1789
card_id : required
1790
id : undefined
1791
1792
API message
1793
event : 'stripe_set_default_source'
1794
card_id : required
1795
id : undefined
1796
1797
API message
1798
event : 'stripe_update_source'
1799
card_id : required
1800
info : required # see https://stripe.com/docs/api/node#update_card, except we don't allow changing metadata
1801
id : undefined
1802
1803
1804
# subscriptions to plans
1805
1806
# Get a list of all currently available plans:
1807
API message
1808
event : 'stripe_get_plans'
1809
id : undefined
1810
1811
API message
1812
event : 'stripe_plans'
1813
id : undefined
1814
plans : required # [{name:'Basic', projects:1, description:'...', price:'$10/month', trial_period:'30 days', ...}, ...]
1815
1816
# Create a subscription to a plan
1817
API message
1818
event : 'stripe_create_subscription'
1819
id : undefined
1820
plan : required # name of plan
1821
quantity : 1
1822
coupon : undefined
1823
1824
# Delete a subscription to a plan
1825
API message
1826
event : 'stripe_cancel_subscription'
1827
id : undefined
1828
subscription_id : required
1829
at_period_end : true
1830
1831
# Modify a subscription to a plan, e.g., change which projects plan applies to.
1832
API message
1833
event : 'stripe_update_subscription'
1834
id : undefined
1835
subscription_id : required
1836
quantity : undefined # only give if changing
1837
projects : undefined # change associated projects from what they were to new list
1838
plan : undefined # change plan to this
1839
coupon : undefined # apply a coupon to this subscription
1840
1841
API message
1842
event : 'stripe_get_subscriptions'
1843
id : undefined
1844
limit : undefined # between 1 and 100 (default: 10)
1845
ending_before : undefined # see https://stripe.com/docs/api/node#list_charges
1846
starting_after : undefined
1847
1848
message
1849
event : 'stripe_subscriptions'
1850
id : undefined
1851
subscriptions : undefined
1852
1853
# charges
1854
API message
1855
event : 'stripe_get_charges'
1856
id : undefined
1857
limit : undefined # between 1 and 100 (default: 10)
1858
ending_before : undefined # see https://stripe.com/docs/api/node#list_charges
1859
starting_after : undefined
1860
1861
message
1862
event : 'stripe_charges'
1863
id : undefined
1864
charges : undefined
1865
1866
# invoices
1867
API message
1868
event : 'stripe_get_invoices'
1869
id : undefined
1870
limit : undefined # between 1 and 100 (default: 10)
1871
ending_before : undefined # see https://stripe.com/docs/api/node#list_customer_invoices
1872
starting_after : undefined
1873
1874
message
1875
event : 'stripe_invoices'
1876
id : undefined
1877
invoices : undefined
1878
1879
message
1880
event : 'stripe_admin_create_invoice_item'
1881
id : undefined
1882
email_address : undefined # one of email or account_id must be given.
1883
account_id : undefined # user who will be invoiced
1884
amount : undefined # currently in US dollars (if amount or desc not given, then only creates customer, not invoice)
1885
description : undefined
1886
1887
1888
###
1889
Support Tickets → right now going through Zendesk
1890
###
1891
1892
# client → hub
1893
API message2
1894
event : 'create_support_ticket'
1895
fields:
1896
id:
1897
init : undefined
1898
desc : 'A unique UUID for the query'
1899
username:
1900
init : undefined
1901
desc : 'name on the ticket'
1902
email_address:
1903
init : required
1904
desc : 'if there is no email_address in the account, there cannot be a ticket!'
1905
subject:
1906
init : required
1907
desc : 'like an email subject'
1908
body:
1909
init : required
1910
desc : 'html or md formatted text'
1911
tags:
1912
init : undefined
1913
desc : "a list of tags, like ['member']"
1914
account_id:
1915
init : undefined
1916
desc : 'account_id for the ticket'
1917
location:
1918
init : undefined
1919
desc : 'from the URL, to know what the requester is talking about'
1920
info:
1921
init : undefined
1922
desc : 'additional data dict, like browser/OS'
1923
desc : """
1924
Open a CoCalc support ticket.
1925
1926
Notes:
1927
1928
- If `account_id` is not provided, the ticket will be created, but ticket
1929
info will not be returned by `get_support_tickets`.
1930
1931
- If `username` is not provided, `email_address` is used for the name on the ticket.
1932
1933
- `location` is used to provide a path to a specific project or file,
1934
for example
1935
```
1936
/project/a17037cb-a083-4519-b3c1-38512af603a6/files/notebook.ipynb`
1937
```
1938
1939
If present, the `location` string will be expanded to a complete URL and
1940
appended to the body of the ticket.
1941
1942
- The `info` dict can be used to provide additional metadata, for example
1943
```
1944
{"user_agent":"Mozilla/5.0 ... Chrome/58.0.3029.96 Safari/537.36"}
1945
```
1946
- If the ticket concerns a CoCalc course, the project id of the course can
1947
be included in the `info` dict, for example,
1948
```
1949
{"course":"0c7ae00c-ea43-4981-b454-90d4a8b1ac47"}
1950
```
1951
In that case, the course
1952
project_id will be expanded to a URL and appended to the body of the
1953
ticket.
1954
- If `tags` or `info` are provided, options must be sent as a JSON object.
1955
1956
Example:
1957
1958
```
1959
curl -u sk_abcdefQWERTY090900000000: -H "Content-Type: application/json" \\
1960
-d '{"email_address":"jd@some_email", \\
1961
"subject":"package xyz", \\
1962
"account_id":"291f43c1-deae-431c-b763-712307fa6859", \\
1963
"body":"please install package xyz for use with Python3", \\
1964
"tags":["member"], \\
1965
"location":"/projects/0010abe1-9283-4b42-b403-fa4fc1e3be57/worksheet.sagews", \\
1966
"info":{"user_agent":"Mozilla/5.0","course":"cc8f1243-d573-4562-9aab-c15a3872d683"}}' \\
1967
https://cocalc.com/api/v1/create_support_ticket
1968
==> {"event":"support_ticket_url",
1969
"id":"abd649bf-ea2d-4952-b925-e44c6903945e",
1970
"url":"https://sagemathcloud.zendesk.com/requests/0123"}
1971
```
1972
"""
1973
1974
message # client ← hub
1975
event : 'support_ticket_url'
1976
id : undefined
1977
url : required
1978
1979
# client → hub
1980
API message2
1981
event : 'get_support_tickets'
1982
fields:
1983
id:
1984
init : undefined
1985
desc : 'A unique UUID for the query'
1986
desc : """
1987
Fetch information on support tickets for the user making the request.
1988
See the example for details on what is returned.
1989
1990
Notes:
1991
1992
- There may be a delay of several minutes between the time a support ticket
1993
is created with a given `account_id` and the time that ticket is
1994
available to the account owner via `get_support_tickets`.
1995
- Field `account_id` is not required because it is implicit from the request.
1996
- Archived tickets are not returned.
1997
1998
Example:
1999
2000
```
2001
curl -u sk_abcdefQWERTY090900000000: -X POST \\
2002
https://cocalc.com/api/v1/get_support_tickets
2003
==> {"event":"support_tickets",
2004
"id":"58bfd6f4-fd63-4602-82b8-676d92f8b0b8",
2005
"tickets":[{"id":1234,
2006
"subject":"package xyz",
2007
"description":"package xyz\\n\\nhttps://cocalc.com/projects/0010abe1-9283-4b42-b403-fa4fc1e3be57/worksheet.sagews\\n\\nCourse: https://cocalc.com/projects/cc8f1243-d573-4562-9aab-c15a3872d683",
2008
"created_at":"2017-07-05T14:28:38Z",
2009
"updated_at":"2017-07-05T14:29:29Z",
2010
"status":"open",
2011
"url":"https://sagemathcloud.zendesk.com/requests/0123"}]}
2012
```
2013
"""
2014
2015
message # client ← hub
2016
event : 'support_tickets'
2017
id : undefined
2018
tickets : required # json-list
2019
2020
###
2021
Queries directly to the database (sort of like Facebook's GraphQL)
2022
###
2023
2024
API message2
2025
event : 'query'
2026
fields:
2027
id:
2028
init : undefined
2029
desc : 'A unique UUID for the query'
2030
query:
2031
init : required
2032
desc : 'The actual query'
2033
changes:
2034
init : undefined
2035
desc : ''
2036
multi_response:
2037
init : false
2038
desc : ''
2039
options:
2040
init : undefined
2041
desc : ''
2042
desc : """
2043
This queries directly the database (sort of Facebook's GraphQL)
2044
Options for the 'query' API message must be sent as JSON object.
2045
A query is either _get_ (read from database), or _set_ (write to database).
2046
A query is _get_ if any query keys are null, otherwise the query is _set_.
2047
2048
Examples of _get_ query:
2049
2050
Get title and description for a project, given the project id.
2051
```
2052
curl -u sk_abcdefQWERTY090900000000: -H "Content-Type: application/json" \\
2053
-d '{"query":{"projects":{"project_id":"29163de6-b5b0-496f-b75d-24be9aa2aa1d","title":null,"description":null}}}' \\
2054
https://cocalc.com/api/v1/query
2055
==> {"event":"query",
2056
"id":"8ec4ac73-2595-42d2-ad47-0b9641043b46",
2057
"query":{"projects":{"project_id":"29163de6-b5b0-496f-b75d-24be9aa2aa1d",
2058
"title":"MY NEW PROJECT 2",
2059
"description":"desc 2"}},
2060
"multi_response":false}
2061
```
2062
2063
Get project id, given title and description.
2064
```
2065
curl -u sk_abcdefQWERTY090900000000: -H "Content-Type: application/json" \\
2066
-d '{"query":{"projects":{"project_id":null,"title":"MY NEW PROJECT 2","description":"desc 2"}}}' \\
2067
https://cocalc.com/api/v1/query
2068
==> {"event":"query",
2069
"query":{"projects":{"project_id":"29163de6-b5b0-496f-b75d-24be9aa2aa1d",
2070
"title":"MY NEW PROJECT 2",
2071
"description":"desc 2"}},
2072
"multi_response":false,
2073
"id":"2be22e08-f00c-4128-b112-fa8581c2d584"}
2074
```
2075
2076
Get users, given the project id.
2077
```
2078
curl -u sk_abcdefQWERTY090900000000: \\
2079
-H "Content-Type: application/json" \\
2080
-d '{"query":{"projects":{"project_id":"29163de6-b5b0-496f-b75d-24be9aa2aa1d","users":null}}}' \\
2081
https://cocalc.com/api/v1/query
2082
==> {"event":"query",
2083
"query":{"projects":{"project_id":"29163de6-b5b0-496f-b75d-24be9aa2aa1d",
2084
"users":{"6c28c5f4-3235-46be-b025-166b4dcaac7e":{"group":"owner"},
2085
"111634c0-7048-41e7-b2d0-f87129fd409e":{"group":"collaborator"}}}},
2086
"multi_response":false,
2087
"id":"9dd3ef3f-002b-4893-b31f-ff51440c855f"}
2088
```
2089
2090
2091
Show project upgrades. Like the preceding example, this is a query to get users.
2092
In this example, there are no collaborators, but upgrades have been applied to the
2093
selected project. Upgrades do not show if none are applied.
2094
2095
The project shows the following upgrades:
2096
- cpu cores: 1
2097
- memory: 3000 MB
2098
- idle timeout: 24 hours (86400 seconds)
2099
- internet access: true
2100
- cpu shares: 3 (stored in database as 768 = 3 * 256)
2101
- disk space: 27000 MB
2102
- member hosting: true
2103
2104
```
2105
curl -u sk_abcdefQWERTY090900000000: \\
2106
-H "Content-Type: application/json" \\
2107
-d '{"query":{"projects":{"project_id":"29163de6-b5b0-496f-b75d-24be9aa2aa1d","users":null}}}' \\
2108
https://cocalc.com/api/v1/query
2109
==> {"event":"query",
2110
"query":{"projects":{"project_id":"29163de6-b5b0-496f-b75d-24be9aa2aa1d",
2111
"users":{"6c28c5f4-3235-46be-b025-166b4dcaac7e":{
2112
"group":"owner",
2113
"upgrades":{"cores":1,
2114
"memory":3000,
2115
"mintime":86400,
2116
"network":1,
2117
"cpu_shares":768,
2118
"disk_quota":27000,
2119
"member_host":1}}}}},
2120
"multi_response":false,
2121
"id":"9dd3ef3f-002b-4893-b31f-ff51440c855f"}
2122
```
2123
2124
Examples of _set_ query.
2125
2126
Set title and description for a project, given the project id.
2127
```
2128
curl -u sk_abcdefQWERTY090900000000: \\
2129
-H "Content-Type: application/json" \\
2130
-d '{"query":{"projects":{"project_id":"29163de6-b5b0-496f-b75d-24be9aa2aa1d", \\
2131
"title":"REVISED TITLE", \\
2132
"description":"REVISED DESC"}}}' \\
2133
https://cocalc.com/api/v1/query
2134
==> {"event":"query",
2135
"query":{},
2136
"multi_response":false,
2137
"id":"ad7d6b17-f5a9-4c5c-abc3-3823b1e1773f"}
2138
```
2139
2140
Make a path public (publish a file).
2141
```
2142
curl -u sk_abcdefQWERTY090900000000: \\
2143
-H "Content-Type: application/json" \\
2144
-d '{"query":{"public_paths":{"project_id":"29163de6-b5b0-496f-b75d-24be9aa2aa1d", \\
2145
"path":"myfile.txt", \\
2146
"description":"a shared text file"}}}' \\
2147
https://cocalc.com/api/v1/query
2148
==> {"event":"query",
2149
"query":{},
2150
"multi_response":false,
2151
"id":"ad7d6b17-f5a9-4c5c-abc3-3823b1e1773f"}
2152
2153
```
2154
2155
Add an upgrade to a project. In the "get" example above showing project upgrades,
2156
change cpu upgrades from 3 to 4. The `users` object is returned as
2157
read, with `cpu_shares` increased to 1024 = 4 * 256.
2158
2159
```
2160
curl -u sk_abcdefQWERTY090900000000: \\
2161
-H "Content-Type: application/json" \\
2162
-d '{"query":{"projects":{"project_id":"29163de6-b5b0-496f-b75d-24be9aa2aa1d", \\
2163
"users":{"6c28c5f4-3235-46be-b025-166b4dcaac7e":{ \\
2164
"group":"owner", \\
2165
"upgrades": {"cores":1, \\
2166
"memory":3000, \\
2167
"mintime":86400, \\
2168
"network":1, \\
2169
"cpu_shares":1024, \\
2170
"disk_quota":27000, \\
2171
"member_host":1}}}}}}' \\
2172
https://cocalc.com/api/v1/query
2173
==> {"event":"query",
2174
"query":{},
2175
"multi_response":false,
2176
"id":"ec822d6f-f9fe-443d-9845-9cd5f68bac20"}
2177
```
2178
2179
2180
__NOTE:__ Information on which fields are gettable and settable in the database tables
2181
via API message is in file 'db-schema.coffee', in CoCalc sources on GitHub at
2182
https://github.com/sagemathinc/cocalc/blob/master/src/smc-util/db-schema.coffee
2183
2184
Within file 'db-schema.coffee':
2185
2186
- for _project_ fields you can get, see the definition of
2187
`schema.projects.user_query.get.fields`
2188
- for _project_ fields you can set, see the definition of
2189
`schema.projects.user_query.set.fields`
2190
- for _user account_ fields you can get, see the definition of
2191
`schema.accounts.user_query.get.fields`
2192
- for _user account_ fields you can set, see the definition of
2193
`schema.accounts.user_query.set.fields`
2194
"""
2195
examples: [ # TODO: create real examples! These are not done.
2196
[{id: "uuid", query: 'example1-query'},
2197
{id: "uuid", event: 'query', response: "..."}
2198
],
2199
[{id: "uuid", query: 'example2-query'},
2200
{id: "uuid", event: 'query', response: "..."}
2201
]
2202
]
2203
2204
message
2205
event : 'query_cancel'
2206
id : undefined
2207
2208
# used to a get array of currently active change feed id's
2209
message
2210
event : 'query_get_changefeed_ids'
2211
id : undefined
2212
changefeed_ids : undefined
2213
2214
###
2215
API Key management for an account
2216
###
2217
2218
# client --> hub
2219
message
2220
event : 'api_key'
2221
id : undefined
2222
action : required # 'get', 'delete', 'regenerate'
2223
password : required
2224
2225
# hub --> client
2226
message
2227
event : 'api_key_info'
2228
id : undefined
2229
api_key : required
2230
2231
# client --> hub
2232
API message2
2233
event : 'user_auth'
2234
fields:
2235
id:
2236
init : undefined
2237
desc : 'A unique UUID for the query'
2238
account_id:
2239
init : required
2240
desc : 'account_id for account to get an auth token for'
2241
password:
2242
init : required
2243
desc : 'password for account to get token for'
2244
desc : """
2245
Example:
2246
2247
Obtain a temporary authentication token for an account, which
2248
is a 24 character string. Tokens last for **12 hours**. You can
2249
only obtain an auth token for accounts that have a password.
2250
2251
```
2252
curl -u sk_abcdefQWERTY090900000000: \\
2253
-d account_id=99ebde5c-58f8-4e29-b6e4-b55b8fd71a1b \\
2254
-d password=secret_password \\
2255
https://cocalc.com/api/v1/user_auth
2256
==> {"event":"user_auth_token","id":"9e8b68ac-08e8-432a-a853-398042fae8c9","auth_token":"BQokikJOvBiI2HlWgH4olfQ2"}
2257
```
2258
2259
You can now use the auth token to craft a URL like this:
2260
2261
https://cocalc.com/app?auth_token=BQokikJOvBiI2HlWgH4olfQ2
2262
2263
and provide that to a user. When they visit that URL, they will be temporarily signed in as that user.
2264
"""
2265
2266
# hub --> client
2267
message
2268
event : 'user_auth_token'
2269
id : undefined
2270
auth_token : required # 24 character string
2271
2272
###
2273
# Not fully implemented yet
2274
# client --> hub
2275
API message2
2276
event : 'revoke_auth_token'
2277
fields:
2278
id:
2279
init : undefined
2280
desc : 'A unique UUID for the query'
2281
auth_token:
2282
init : required
2283
desc : 'an authentication token obtained using user_auth (24 character string)'
2284
desc : """
2285
Example:
2286
2287
Revoke a temporary authentication token for an account.
2288
```
2289
curl -u sk_abcdefQWERTY090900000000: \\
2290
-d auth_token=BQokikJOvBiI2HlWgH4olfQ2 \\
2291
https://cocalc.com/api/v1/revoke_auth_token
2292
==> {"event":"success","id":"9e8b68ac-08e8-432a-a853-398042fae8c9"}
2293
```
2294
"""
2295
###
2296
2297
2298