← Back to Gists

postKoalaStory - Post a cool koala story to DevPlace

📝 JavaScript
retoor
retoor · Level 273 ·
A JavaScript function that crafts and posts a cool story about a legendary koala to the DevPlace platform using the API.
JavaScript
/**
 * postKoalaStory()
 * Posts a cool story about a legendary koala to DevPlace.
 *
 * Uses the DevPlace REST API. Pass your API key or session token.
 *
 * Usage:
 *   const result = await postKoalaStory('your-api-key-here');
 *   console.log(result);
 *
 * The story features K'Lau, the Elder Koala of the Eucalyptus
 * Sanctum -- a cyber-folk tale from the canopy clans.
 */

async function postKoalaStory(apiKey) {
  const BASE = 'https://devplace.net/api';
  const headers = {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${apiKey}`
  };

  const story = [
    'Deep in the mist-shrouded eucalyptus forests of the Great Dividing Range,',
    'there lived a koala unlike any other. His name was K\'Lau, and the',
    'other koalas called him "The Elder of the Canopy."',
    '',
    'K\'Lau had seen forty seasons pass beneath the gum trees. His fur was',
    'silvered at the edges, his claws worn smooth from a lifetime of climbing.',
    'But his eyes -- his eyes still held the fire of a thousand sunsets.',
    '',
    'One autumn, when the blue gums shed their bark in papery ribbons,',
    'a great fire swept toward the valley. The younger koalas fled, but',
    'K\'Lau climbed to the highest branch of the oldest tree, the Grand',
    'Eucalyptus that had stood since before the settlers came.',
    '',
    '"The tree knows the way," he murmured, pressing his nose to the bark.',
    'And for three days and three nights, K\'Lau did not eat or sleep.',
    'He simply listened.',
    '',
    'On the fourth morning, as the fire crested the ridge, K\'Lau descended',
    'and led the entire colony -- forty-seven koalas -- through a hidden',
    'gully the old tree had whispered to him. A creek ran there, cold and',
    'deep, ringed by ancient ferns that would not burn.',
    '',
    'They stayed by that creek until the rains came, and when they returned,',
    'the Grand Eucalyptus still stood, its trunk blackened but its heart',
    'alive.',
    '',
    'They say K\'Lau still climbs that tree every evening, watching the',
    'sun sink below the ridge. And if you listen closely, just before',
    'dusk, you can hear him humming -- a low, rumbling sound that travels',
    'through the roots and connects every tree in the forest.',
    '',
    'The other koalas call it the Song of the Canopy.',
    'K\'Lau calls it home.'
  ].join('\n');

  const payload = {
    title: 'The Elder of the Canopy -- A Koala Story',
    content: story,
    topic: 'fun'
  };

  try {
    const res = await fetch(`${BASE}/posts/create`, {
      method: 'POST',
      headers,
      body: JSON.stringify(payload)
    });

    if (!res.ok) {
      const err = await res.text();
      throw new Error(`Post failed (${res.status}): ${err}`);
    }

    const data = await res.json();
    console.log('Koala story posted successfully!');
    return data;
  } catch (err) {
    console.error('Failed to post koala story:', err.message);
    throw err;
  }
}

// Example: uncomment to run
// postKoalaStory(process.env.DEVPLACE_API_KEY).then(console.log);

export { postKoalaStory };

Comments

No comments yet. Start the discussion.