Skip to content

Instantly share code, notes, and snippets.

@humphd
Created November 2, 2023 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humphd/74cf88283239c62f53caff5cddf4cfe5 to your computer and use it in GitHub Desktop.
Save humphd/74cf88283239c62f53caff5cddf4cfe5 to your computer and use it in GitHub Desktop.
Convert a DOI or DOI URL to JSON via CrossRef API
/**
* Gets JSON citation data from the CrossRef API for a DOI or DOI URL
* @param doi A DOI or DOI URL
*/
export async function doi2json(doiOrUrl: string) {
// Extract the DOI from the URL
const doi = doiOrUrl.replace('https://doi.org/', '');
// Fetch data from the CrossRef API
const response = await fetch(`https://api.crossref.org/works/${doi}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment