.. _api: === API === Starting from RhodeCode version 1.2 a simple API was implemented. There's a single schema for calling all api methods. API is implemented with JSON protocol both ways. An url to send API request in RhodeCode is /_admin/api API ACCESS FOR WEB VIEWS ++++++++++++++++++++++++ API access can also be turned on for each web view in RhodeCode that is decorated with `@LoginRequired` decorator. To enable API access simple change the standard login decorator to `@LoginRequired(api_access=True)`. After this change, a rhodecode view can be accessed without login by adding a GET parameter `?api_key=` to url. By default this is only enabled on RSS/ATOM feed views. API ACCESS ++++++++++ All clients are required to send JSON-RPC spec JSON data:: { "id:"", "api_key":"", "method":"", "args":{"":""} } Example call for autopulling remotes repos using curl:: curl https://server.com/_admin/api -X POST -H 'content-type:text/plain' --data-binary '{"id":1,"api_key":"xe7cdb2v278e4evbdf5vs04v832v0efvcbcve4a3","method":"pull","args":{"repo":"CPython"}}' Simply provide - *id* A value of any type, which is used to match the response with the request that it is replying to. - *api_key* for access and permission validation. - *method* is name of method to call - *args* is an key:value list of arguments to pass to method .. note:: api_key can be found in your user account page RhodeCode API will return always a JSON-RPC response:: { "id":, # matching id sent by request "result": ""|null, # JSON formatted result, null if any errors "error": "null"| # JSON formatted error (if any) } All responses from API will be `HTTP/1.0 200 OK`, if there's an error while calling api *error* key from response will contain failure description and result will be null. API CLIENT ++++++++++ From version 1.4 RhodeCode adds a binary script that allows to easily communicate with API. After installing RhodeCode a `rhodecode-api` script will be available. To get started quickly simply run:: rhodecode-api _create_config --apikey= --apihost= This will create a file named .config in the directory you executed it storing json config file with credentials. You can skip this step and always provide both of the arguments to be able to communicate with server after that simply run any api command for example get_repo:: rhodecode-api get_repo calling {"api_key": "", "id": 75, "args": {}, "method": "get_repo"} to http://127.0.0.1:5000 rhodecode said: {'error': 'Missing non optional `repoid` arg in JSON DATA', 'id': 75, 'result': None} Ups looks like we forgot to add an argument Let's try again now giving the repoid as parameters:: rhodecode-api get_repo repoid:rhodecode calling {"api_key": "", "id": 39, "args": {"repoid": "rhodecode"}, "method": "get_repo"} to http://127.0.0.1:5000 rhodecode said: {'error': None, 'id': 39, 'result': } API METHODS +++++++++++ pull ---- Pulls given repo from remote location. Can be used to automatically keep remote repos up to date. This command can be executed only using api_key belonging to user with admin rights INPUT:: id : api_key : "" method : "pull" args : { "repo_name" : "" } OUTPUT:: result : "Pulled from " error : null get_user -------- Get's an user by username or user_id, Returns empty result if user is not found. This command can be executed only using api_key belonging to user with admin rights. INPUT:: id : api_key : "" method : "get_user" args : { "userid" : "" } OUTPUT:: result: None if user does not exist or { "id" : "", "username" : "", "firstname": "", "lastname" : "", "email" : "", "active" : "", "admin" :  "", "ldap_dn" : "", "last_login": "", "permissions": { "global": ["hg.create.repository", "repository.read", "hg.register.manual_activate"], "repositories": {"repo1": "repository.none"}, "repositories_groups": {"Group1": "group.read"} }, } error: null get_users --------- Lists all existing users. This command can be executed only using api_key belonging to user with admin rights. INPUT:: id : api_key : "" method : "get_users" args : { } OUTPUT:: result: [ { "id" : "", "username" : "", "firstname": "", "lastname" : "", "email" : "", "active" : "", "admin" :  "", "ldap_dn" : "", "last_login": "", }, … ] error: null create_user ----------- Creates new user. This command can be executed only using api_key belonging to user with admin rights. INPUT:: id : api_key : "" method : "create_user" args : { "username" : "", "password" : "", "email" : "", "firstname" : " = None", "lastname" : " = None", "active" : " = True", "admin" : " = False", "ldap_dn" : " = None" } OUTPUT:: result: { "id" : "", "msg" : "created new user ", "user": { "id" : "", "username" : "", "firstname": "", "lastname" : "", "email" : "", "active" : "", "admin" :  "", "ldap_dn" : "", "last_login": "", }, } error: null update_user ----------- updates given user if such user exists. This command can be executed only using api_key belonging to user with admin rights. INPUT:: id : api_key : "" method : "update_user" args : { "userid" : "", "username" : "", "password" : "", "email" : "", "firstname" : "", "lastname" : "", "active" : "", "admin" : "", "ldap_dn" : "" } OUTPUT:: result: { "id" : "", "msg" : "updated user ID: " } error: null delete_user ----------- deletes givenuser if such user exists. This command can be executed only using api_key belonging to user with admin rights. INPUT:: id : api_key : "" method : "delete_user" args : { "userid" : "", } OUTPUT:: result: { "id" : "", "msg" : "deleted user ID: " } error: null get_users_group --------------- Gets an existing users group. This command can be executed only using api_key belonging to user with admin rights. INPUT:: id : api_key : "" method : "get_users_group" args : { "group_name" : "" } OUTPUT:: result : None if group not exist { "id" : "", "group_name" : "", "active": "", "members" : [ { "id" : "", "username" : "", "firstname": "", "lastname" : "", "email" : "", "active" : "", "admin" :  "", "ldap" : "" }, … ] } error : null get_users_groups ---------------- Lists all existing users groups. This command can be executed only using api_key belonging to user with admin rights. INPUT:: id : api_key : "" method : "get_users_groups" args : { } OUTPUT:: result : [ { "id" : "", "group_name" : "", "active": "", "members" : [ { "id" : "", "username" : "", "firstname": "", "lastname" : "", "email" : "", "active" : "", "admin" :  "", "ldap" : "" }, … ] } ] error : null create_users_group ------------------ Creates new users group. This command can be executed only using api_key belonging to user with admin rights INPUT:: id : api_key : "" method : "create_users_group" args: { "group_name": "", "active":" = True" } OUTPUT:: result: { "id": "", "msg": "created new users group " } error: null add_user_to_users_group ----------------------- Adds a user to a users group. If user exists in that group success will be `false`. This command can be executed only using api_key belonging to user with admin rights INPUT:: id : api_key : "" method : "add_user_users_group" args: { "group_name" : "", "username" : "" } OUTPUT:: result: { "id": "", "success": True|False # depends on if member is in group "msg": "added member to users group | User is already in that group" } error: null remove_user_from_users_group ---------------------------- Removes a user from a users group. If user is not in given group success will be `false`. This command can be executed only using api_key belonging to user with admin rights INPUT:: id : api_key : "" method : "remove_user_from_users_group" args: { "group_name" : "", "username" : "" } OUTPUT:: result: { "success": True|False, # depends on if member is in group "msg": "removed member from users group | User wasn't in group" } error: null get_repo -------- Gets an existing repository by it's name or repository_id. Members will return either users_group or user associated to that repository. This command can be executed only using api_key belonging to user with admin rights. INPUT:: id : api_key : "" method : "get_repo" args: { "repoid" : "" } OUTPUT:: result: None if repository does not exist or { "id" : "", "repo_name" : "" "type" : "", "description" : "", "clone_uri" : "", "private": : "", "created_on" : "", "members" : [ { "type": "user", "id" : "", "username" : "", "firstname": "", "lastname" : "", "email" : "", "active" : "", "admin" :  "", "ldap" : "", "permission" : "repository.(read|write|admin)" }, … { "type": "users_group", "id" : "", "name" : "", "active": "", "permission" : "repository.(read|write|admin)" }, … ] } error: null get_repos --------- Lists all existing repositories. This command can be executed only using api_key belonging to user with admin rights INPUT:: id : api_key : "" method : "get_repos" args: { } OUTPUT:: result: [ { "id" : "", "repo_name" : "" "type" : "", "description" : "", "clone_uri" : "", "private": : "", "created_on" : "", }, … ] error: null get_repo_nodes -------------- returns a list of nodes and it's children in a flat list for a given path at given revision. It's possible to specify ret_type to show only `files` or `dirs`. This command can be executed only using api_key belonging to user with admin rights INPUT:: id : api_key : "" method : "get_repo_nodes" args: { "repo_name" : "", "revision" : "", "root_path" : "", "ret_type" : "" = 'all' } OUTPUT:: result: [ { "name" : "" "type" : "", }, … ] error: null create_repo ----------- Creates a repository. This command can be executed only using api_key belonging to user with admin rights. If repository name contains "/", all needed repository groups will be created. For example "foo/bar/baz" will create groups "foo", "bar" (with "foo" as parent), and create "baz" repository with "bar" as group. INPUT:: id : api_key : "" method : "create_repo" args: { "repo_name" : "", "owner_name" : "", "description" : " = ''", "repo_type" : " = 'hg'", "private" : " = False", "clone_uri" : " = None", } OUTPUT:: result: { "id": "", "msg": "Created new repository ", "repo": { "id" : "", "repo_name" : "" "type" : "", "description" : "", "clone_uri" : "", "private": : "", "created_on" : "", }, } error: null delete_repo ----------- Deletes a repository. This command can be executed only using api_key belonging to user with admin rights. INPUT:: id : api_key : "" method : "delete_repo" args: { "repo_name" : "", } OUTPUT:: result: { "msg": "Deleted repository ", } error: null grant_user_permission --------------------- Grant permission for user on given repository, or update existing one if found. This command can be executed only using api_key belonging to user with admin rights. INPUT:: id : api_key : "" method : "grant_user_permission" args: { "repo_name" : "", "username" : "", "perm" : "(repository.(none|read|write|admin))", } OUTPUT:: result: { "msg" : "Granted perm: for user: in repo: " } error: null revoke_user_permission ---------------------- Revoke permission for user on given repository. This command can be executed only using api_key belonging to user with admin rights. INPUT:: id : api_key : "" method : "revoke_user_permission" args: { "repo_name" : "", "username" : "", } OUTPUT:: result: { "msg" : "Revoked perm for user: in repo: " } error: null grant_users_group_permission ---------------------------- Grant permission for users group on given repository, or update existing one if found. This command can be executed only using api_key belonging to user with admin rights. INPUT:: id : api_key : "" method : "grant_users_group_permission" args: { "repo_name" : "", "group_name" : "", "perm" : "(repository.(none|read|write|admin))", } OUTPUT:: result: { "msg" : "Granted perm: for group: in repo: " } error: null revoke_users_group_permission ----------------------------- Revoke permission for users group on given repository.This command can be executed only using api_key belonging to user with admin rights. INPUT:: id : api_key : "" method : "revoke_users_group_permission" args: { "repo_name" : "", "users_group" : "", } OUTPUT:: result: { "msg" : "Revoked perm for group: in repo: " } error: null