# Using /rules/create API

**URL:** <https://community.sonarsource.com/t/using-rules-create-api/1243>\
**Category:** SonarQube Server / Community Build\
**Tags:** sonarqube\
**Created:** [July 23, 2018, 1:08pm UTC](https://community.sonarsource.com/t/using-rules-create-api/1243 "2018-07-23T13:08:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![WaldoFR](https://avatars.discourse-cdn.com/v4/letter/w/da6949/32.png) [@WaldoFR](https://community.sonarsource.com/u/WaldoFR)\
**Post date:** [July 23, 2018, 1:08pm UTC](https://community.sonarsource.com/t/using-rules-create-api/1243/1 "2018-07-23T13:08:53Z")

</div>

Must-share information:

- Sonarqube 6.7.1, 6.7.4
- Using /api/rules API
- I never succeeded in using this API

First of all, it seems that parameters documentation isn’t right, as it’s shown that only the `key` is required while finally the ̀template\_key` parameter is also requried.

Also, after many try I couldn’t successfully realize a good call (curl). I tried many way and when I fix the request using all required parameters in the request it fails to work, e.g. :

```auto
curl -v -su admin:admin -X POST 'localhost:9000/api/rules/create?custom_key="cat"&template_key="dog"'
 
curl -su admin:admin \
  --header "Content-Type: application/json" \
  --request POST \
  --data '{"custom_key":"checkstyle:Custom_InnerAssignmentCheck","markdown_description":"Checks for assignments in subexpressions, such as in String s = Integer.toString(i = 2);.\n\nThis custom rule inherits from Inner Assignment template.","name":"Custom Inner Assignement","severity":"MAJOR","status":"READY","template_key":"chekstyle:com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck","type":"CODE_SMELL"}' \
  http://localhost:9000/api/rules/create

curl -v -su admin:admin -X POST 'localhost:9000/api/rules/create?custom_key="checkstyle:Custom_InnerAssignmentCheck"&markdown_description="Checks for assignments in subexpressions, such as in String s = Integer.toString(i = 2);.\n\nThis custom rule inherits from Inner Assignment template."&name="Custom Inner Assignment"&severity="MAJOR"&status="READY"&template_key="checkstyle:com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck"&type="CODE_SMELL"'

```

---

<div class="post-metadata">

**Author:** ![Gregoire\_Aubert](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/gregoire_aubert/32/29_2.png) [@Gregoire\_Aubert](https://community.sonarsource.com/u/Gregoire_Aubert)\
**Post date:** [July 24, 2018, 8:05am UTC](https://community.sonarsource.com/t/using-rules-create-api/1243/2 "2018-07-24T08:05:05Z")

</div>

Hi Waldo,  
Indeed it seems that this web-service has a bad documentation and we have a ticket to improve it: [SONAR-9539](https://jira.sonarsource.com/browse/SONAR-9539) (make sure to vote for it).

In the mean time, on my side, I was able to create custom rules with the following curl request:

```auto
curl -su admin:admin \
  -X POST \
  -d 'template_key=javascript%3ACommentRegularExpression&custom_key=customRuleTest&name=customRuleTest&markdown_description=My%20custom%20rule&severity=MAJOR' \
  'http://localhost:9000/api/rules/create'

```

All those 5 parameters are required to be able to create a custom rule, also note that `template_key` must be the key of an existing template rule.

If you are still not able to perform a successful request, please also send here the answer from the web-service, there is an error message that should help understand the problem.

---

<div class="post-metadata">

**Author:** ![jagadeeswar](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jagadeeswar](https://community.sonarsource.com/u/jagadeeswar)\
**Post date:** [March 18, 2021, 9:18am UTC](https://community.sonarsource.com/t/using-rules-create-api/1243/3 "2021-03-18T09:18:52Z")

</div>

@Gregoire_Aubert Thanks for the command that really helped me too.

Is it possible to create rules using json file ? I am trying to create by passing arguments in json file but it is not able to recognize.

===  
$ curl -su admin:admin -X POST -d @sonarqube.json ‘[https://sonarque/api/rules/create](https://sonarque/api/rules/create)’ --header “Content-Type: application/json”

{“errors”:[{“msg”:“The ‘custom\_key’ parameter is missing”}]}

===

sonarqube.json file contains below data.

==  
$ cat sonarqube.json  
{  
“custom\_key”:“testrule2”,  
“name”:“Custom Inner Assignement”,  
“severity”:“MAJOR”,  
“template\_key”:“ClangTidy%3ACustomRuleTemplate”,  
“type”:“CODE\_SMELL”  
}
