# Shorten a New Link

<mark style="color:green;">`POST`</mark> `https://sh.phyr.in/api/generate`

Creates a new link

#### Request Body

<table><thead><tr><th>Name</th><th width="160">Type</th><th>Description</th></tr></thead><tbody><tr><td>url<mark style="color:red;">*</mark></td><td>string</td><td><code>URL</code> to be shortened</td></tr><tr><td>publicId</td><td>string</td><td>Custom alias to be used</td></tr><tr><td>password</td><td>string</td><td>Custom password </td></tr></tbody></table>

#### Request JSON

```json
{
    "url": "https://www.example.com",
    "publicId": "hello",
    "password": "123456"
}
```

{% tabs %}
{% tab title="200 Link successfully created" %}

```json
{
    "success": true,
    "link": {
        "publicId": "hello",
        "analyticsId": "mICTFx"
    },
    "message": "Link created successfully"
}
```

{% endtab %}

{% tab title="200 Missing required fileds " %}

```json
{
    "success": false,
    "message": "Missing required fields. URL"
}
```

{% endtab %}
{% endtabs %}

Take a look at how you might call this method using `curl` and `JS`:

{% tabs %}
{% tab title="curl" %}

```
curl --location 'https://sh.phyr.in/api/generate' \
--header 'Content-Type: application/json' \
--data '{
    "url": "https://www.example.com",
    "publicId": "hello",
    "password": "123456"
}'
```

{% endtab %}

{% tab title="Javascript ( Fetch )" %}

```javascript
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");

const raw = JSON.stringify({
  "url": "https://www.example.com",
  "publicId": "hello",
  "password": "123456"
});

const requestOptions = {
  method: "POST",
  headers: myHeaders,
  body: raw,
  redirect: "follow"
};

fetch("https://sh.phyr.in/api/generate", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
```

{% endtab %}

{% tab title="Node.js ( Axios )" %}

```javascript
const axios = require('axios');
let data = JSON.stringify({
  "url": "https://www.example.com",
  "publicId": "hello",
  "password": "123456"
});

let config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://sh.phyr.in/api/generate',
  headers: { 
    'Content-Type': 'application/json'
  },
  data : data
};

axios.request(config)
.then((response) => {
  console.log(JSON.stringify(response.data));
})
.catch((error) => {
  console.log(error);
});

```

{% endtab %}
{% endtabs %}

### Possible <mark style="color:red;">errors</mark>

1. Custom alias exists: The publicId sent in the request body has been already used. Please use another publicID or keep it empty to generate a random alias.
