> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://learning.postman.com/llms.txt.

# Create a custom function

POST https://api.postman.com/custom-functions
Content-Type: application/json

Creates a new custom function for the team.

Reference: https://learning.postman.com/api-docs/api-reference/api-governance/create-custom-function

## Authentication

- `x-api-key` header (required) — API Key authentication via header

## Servers

- `https://api.postman.com` (https://api.postman.com, default)
- `https://api.eu.postman.com` (https://api.eu.postman.com)

## Request

### Body (application/json)

This endpoint expects a customFunctionCreate.

- `path` (string, required) — The custom function's path, in the form `functions/<name>.js`. Root-level paths and nested paths are not supported. The name must be at least two characters, start with a letter, underscore, or dollar sign, and contain only letters, numbers, underscores, and dollar signs.
- `content` (string, required) — The custom function's content, up to a maximum of 500 KB (UTF-8).
- `description` (string, optional) — The custom function's description.

## Response

### 201

Custom Function Created

- `id` (string, required) — The resource's ID.

## Errors

### 400 Bad Request Error

Bad Request

- `CreateCustomFunctionRequestBadRequestError`

### 401 Unauthorized Error

Unauthorized

- `CreateCustomFunctionRequestUnauthorizedError`

### 403 Forbidden Error

Forbidden

- `CreateCustomFunctionRequestForbiddenError`

### 409 Conflict Error

Conflict

- `type` (CommonErrorTypeTitleDetailStatusType, optional) — The type of error.
- `title` (string, optional) — A short summary of the problem.
- `detail` (string, optional) — Information about the error.
- `status` (integer, optional) — The error's HTTP status code.

### 500 Internal Server Error

Internal Server Error

- `CreateCustomFunctionRequestInternalServerError`

## Types

### commonErrorTypeTitleDetail

- `type` (string, optional) — The type of error.
- `title` (string, optional) — A short summary of the problem.
- `detail` (CommonErrorTypeTitleDetailDetail, optional) — Information about the error.

### commonErrorTypeTitleDetailStatus

- `type` (CommonErrorTypeTitleDetailStatusType, optional) — The type of error.
- `title` (string, optional) — A short summary of the problem.
- `detail` (string, optional) — Information about the error.
- `status` (integer, optional) — The error's HTTP status code.

### commonErrorNameMessageDetails

- `error` (CommonErrorNameMessageDetailsError, optional) — Information about the error.

### commonErrorNameMessage

- `error` (CommonErrorNameMessageError, optional) — Information about the error.

### CommonErrorTypeTitleDetailStatusType

The type of error.

### CommonErrorTypeTitleDetailDetail

Information about the error.

### CommonErrorNameMessageDetailsError

Information about the error.

- `name` (string, optional) — The error name.
- `message` (string, optional) — The error message.
- `details` (CommonErrorNameMessageDetailsErrorDetails, optional) — Information about the error.

### CommonErrorNameMessageError

Information about the error.

- `name` (string, optional) — The error name.
- `message` (string, optional) — The error message.

### CommonErrorNameMessageDetailsErrorDetails

Information about the error.

## Examples

### Custom Function Created

**Request**

```json
undefined
```

**Response**

```json
{
  "id": "01JCUSTOMFN0123456789ABCDE"
}
```

**SDK Code**

```python Custom Function Created
import requests

url = "https://api.postman.com/custom-functions"

headers = {"x-api-key": "<apiKey>"}

response = requests.post(url, headers=headers)

print(response.json())
```

```javascript Custom Function Created
const url = 'https://api.postman.com/custom-functions';
const options = {method: 'POST', headers: {'x-api-key': '<apiKey>'}};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go Custom Function Created
package main

import (
	"fmt"
	"net/http"
	"io"
)

func main() {

	url := "https://api.postman.com/custom-functions"

	req, _ := http.NewRequest("POST", url, nil)

	req.Header.Add("x-api-key", "<apiKey>")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby Custom Function Created
require 'uri'
require 'net/http'

url = URI("https://api.postman.com/custom-functions")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<apiKey>'

response = http.request(request)
puts response.read_body
```

```java Custom Function Created
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.postman.com/custom-functions")
  .header("x-api-key", "<apiKey>")
  .asString();
```

```php Custom Function Created
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.postman.com/custom-functions', [
  'headers' => [
    'x-api-key' => '<apiKey>',
  ],
]);

echo $response->getBody();
```

```csharp Custom Function Created
using RestSharp;

var client = new RestClient("https://api.postman.com/custom-functions");
var request = new RestRequest(Method.POST);
request.AddHeader("x-api-key", "<apiKey>");
IRestResponse response = client.Execute(request);
```

```swift Custom Function Created
import Foundation

let headers = ["x-api-key": "<apiKey>"]

let request = NSMutableURLRequest(url: NSURL(string: "https://api.postman.com/custom-functions")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```

### Create Custom Function

**Request**

```json
{
  "path": "functions/checkResourceNesting.js",
  "content": "export default (input) => { return []; };",
  "description": "Custom Spectral function."
}
```

**Response**

```json
{
  "id": "01JCUSTOMFN0123456789ABCDE"
}
```

**SDK Code**

```python Create Custom Function
import requests

url = "https://api.postman.com/custom-functions"

payload = {
    "path": "functions/checkResourceNesting.js",
    "content": "export default (input) => { return []; };",
    "description": "Custom Spectral function."
}
headers = {
    "x-api-key": "<apiKey>",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript Create Custom Function
const url = 'https://api.postman.com/custom-functions';
const options = {
  method: 'POST',
  headers: {'x-api-key': '<apiKey>', 'Content-Type': 'application/json'},
  body: '{"path":"functions/checkResourceNesting.js","content":"export default (input) => { return []; };","description":"Custom Spectral function."}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go Create Custom Function
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.postman.com/custom-functions"

	payload := strings.NewReader("{\n  \"path\": \"functions/checkResourceNesting.js\",\n  \"content\": \"export default (input) => { return []; };\",\n  \"description\": \"Custom Spectral function.\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("x-api-key", "<apiKey>")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby Create Custom Function
require 'uri'
require 'net/http'

url = URI("https://api.postman.com/custom-functions")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<apiKey>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"path\": \"functions/checkResourceNesting.js\",\n  \"content\": \"export default (input) => { return []; };\",\n  \"description\": \"Custom Spectral function.\"\n}"

response = http.request(request)
puts response.read_body
```

```java Create Custom Function
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.postman.com/custom-functions")
  .header("x-api-key", "<apiKey>")
  .header("Content-Type", "application/json")
  .body("{\n  \"path\": \"functions/checkResourceNesting.js\",\n  \"content\": \"export default (input) => { return []; };\",\n  \"description\": \"Custom Spectral function.\"\n}")
  .asString();
```

```php Create Custom Function
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.postman.com/custom-functions', [
  'body' => '{
  "path": "functions/checkResourceNesting.js",
  "content": "export default (input) => { return []; };",
  "description": "Custom Spectral function."
}',
  'headers' => [
    'Content-Type' => 'application/json',
    'x-api-key' => '<apiKey>',
  ],
]);

echo $response->getBody();
```

```csharp Create Custom Function
using RestSharp;

var client = new RestClient("https://api.postman.com/custom-functions");
var request = new RestRequest(Method.POST);
request.AddHeader("x-api-key", "<apiKey>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"path\": \"functions/checkResourceNesting.js\",\n  \"content\": \"export default (input) => { return []; };\",\n  \"description\": \"Custom Spectral function.\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift Create Custom Function
import Foundation

let headers = [
  "x-api-key": "<apiKey>",
  "Content-Type": "application/json"
]
let parameters = [
  "path": "functions/checkResourceNesting.js",
  "content": "export default (input) => { return []; };",
  "description": "Custom Spectral function."
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.postman.com/custom-functions")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```