Skip to content

GraphQL Examples

End-to-End: Shorten a URL Then Read Its Stats

1. Login

bash
TOKEN=$(curl -s -X POST https://lnkify.io/graphql \
  -H "Content-Type: application/json" \
  -d '{"query": "mutation { login(email: \"user@example.com\", password: \"your-pass\") { token } }"}' \
  | jq -r '.data.login.token')
bash
SLUG=$(curl -s -X POST https://lnkify.io/graphql \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{"query": "mutation { createLnkify(target: \"https://example.com/my-long-page\", lnkify: \"my-page\", title: \"My Page\", enableTracking: true) }"}' \
  | jq -r '.data.createLnkify')

echo "Shortlink: https://lnkify.io/$SLUG"
bash
curl -s -X POST https://lnkify.io/graphql \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{"query": "query { lnkifyConnection(skip: 0, take: 50) { items { id lnkify target title hitCount } totalCount } }"}' \
  | jq '.data.lnkifyConnection.items'
bash
# First get the ID
ID=$(curl -s -X POST https://lnkify.io/graphql \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d "{\"query\": \"query { lnkifyConnection(skip: 0, take: 100) { items { id lnkify } } }\"}" \
  | jq -r ".data.lnkifyConnection.items[] | select(.lnkify==\"$SLUG\") | .id")

# Fetch full stats
curl -s -X POST https://lnkify.io/graphql \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d "{\"query\": \"query { getLnkifyInfo(id: \\\"$ID\\\") { id lnkify target hitCount hits { ip browser { name } location { city country { name } } createdAt } byCountryGraph { count country { name } } } }\"}" \
  | jq '.data.getLnkifyInfo'

JavaScript Fetch

js
const ENDPOINT = "https://lnkify.io/graphql";

async function graphql(query, token) {
  const response = await fetch(ENDPOINT, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "Authorization": `Bearer ${token}`
    },
    body: JSON.stringify({ query })
  });
  const result = await response.json();

  if (result.errors) {
    for (const err of result.errors) {
      console.error(`[${err.extensions?.code}] ${err.message}`);
    }
  }

  return result.data;
}

async function main() {
  // Login
  const { login } = await graphql(
    `mutation { login(email: "user@example.com", password: "pass") { token } }`,
    null
  );

  // Create
  const { createLnkify } = await graphql(
    `mutation { createLnkify(target: "https://example.com") }`,
    login.token
  );
  console.log(`Created: https://lnkify.io/${createLnkify}`);

  // List
  const { lnkifyConnection } = await graphql(
    `query { lnkifyConnection(skip: 0, take: 50) { items { id lnkify target hitCount } } }`,
    login.token
  );
  console.table(lnkifyConnection.items);
}

main();

API Key Auth (x-api-key)

bash
curl -X POST https://lnkify.io/graphql \
  -H "Content-Type: application/json" \
  -H "x-api-key: lf_live_a1b2c3d4e5f6g7h8" \
  -d '{"query": "query { getUserInfo { name email } }"}'
js
// JavaScript
const response = await fetch("https://lnkify.io/graphql", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "x-api-key": "lf_live_a1b2c3d4e5f6g7h8"
  },
  body: JSON.stringify({
    query: `query { lnkifyConnection(skip: 0, take: 50) { items { id lnkify target } } }`
  })
});
const { data, errors } = await response.json();

Bulk Create

bash
curl -s -X POST https://lnkify.io/graphql \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "query": "mutation($inputs: [CreateLnkifyInput!]!) { createLnkifies(inputs: $inputs) { index ok lnkify { id lnkify target } error } }",
    "variables": {
      "inputs": [
        { "target": "https://example.com/1", "lnkify": "one", "title": "First" },
        { "target": "https://example.com/2", "lnkify": "two", "enableTracking": true },
        { "target": "https://example.com/3", "lnkify": "three" }
      ]
    }
  }' \
  | jq '.data.createLnkifies'

Error Handling

bash
# Try to create with a missing required field
curl -s -X POST https://lnkify.io/graphql \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{"query": "mutation { createLnkify }"}' \
  | jq '.'

Response:

json
{
  "data": null,
  "errors": [
    {
      "message": "Field \"createLnkify\" argument \"target\" of type \"String!\" is required, but it was not provided.",
      "extensions": { "code": "BAD_USER_INPUT" }
    }
  ]
}
bash
# Try an unauthenticated request
curl -s -X POST https://lnkify.io/graphql \
  -H "Content-Type: application/json" \
  -d '{"query": "query { lnkifyConnection(skip: 0, take: 10) { items { id } } }"}' \
  | jq '.'

Response:

json
{
  "data": null,
  "errors": [
    {
      "message": "You must be logged in.",
      "extensions": { "code": "UNAUTHENTICATED" },
      "path": ["lnkifyConnection"]
    }
  ]
}

Working with the Redirect

After creating a shortlink with slug "my-link", users can visit it directly:

https://lnkify.io/my-link

This issues a 302 redirect to the target URL. If enableTracking was set to true, each visit records a hit event and increments hitCount.

Released under the MIT License.